Jump to content

[SOLVED] Need help with how to code something...


Recommended Posts

Hello, I... need help :)........... So on a certain event i'm trying to get all trees around my character to start playing anims as if they're being chopped (but not really getting chopped) but I have no Idea how to do that since trees I think aren't entities so I can't do something like...

local x,y,z = inst.Transform:GetWorldPosition()                                                                                                  --
local ents = TheSim:FindEntities(x, y, z, 20, { "_combat" }, { "player", "shadow", "shadowminion" })                                             --
for i,v in ipairs(ents) do                                                                                                                       --
if v:HasTag("tree") then                                                                                           --                                                                                                                                           --
v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.44, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --                                                              --
end                                                                                                                                              --
end                                                                                                                                              --

So how would I do that? It's okay if it's not possible then please tell me then I can delete this thread because I feel ashamed to put up so many threads all asking for help :?...

And thanks for reading my problem I hope you have a wonderful day/night :D!!!!

 

PS: If someone also know how would I get this to work? I'm trying to also make spider houses look like they're being hit so I went in the spiderden.lua for when it gets hit & saw this "inst.AnimState:PlayAnimation(inst.anims.hit)" but it just crashes the game why?!

local x,y,z = inst.Transform:GetWorldPosition()                                                                                                  --
local ents = TheSim:FindEntities(x, y, z, 20, { "_combat" }, { "player", "shadow", "shadowminion" })                                             --
for i,v in ipairs(ents) do                                                                                                                       --
if v:HasTag("spiderden") then                                                                                           --
if v.components.health:IsDead() then                                                                                                             --
return                                                                                                                                           --
end                                                                                                                                              --
v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --           
end                                                                                                                                              --
end                                                                                                                                              --

 

Edited by SuperDavid
Link to comment
Share on other sites

3 hours ago, SuperDavid said:

Hello, I... need help :)........... So on a certain event i'm trying to get all trees around my character to start playing anims as if they're being chopped (but not really getting chopped) but I have no Idea how to do that since trees I think aren't entities so I can't do something like...


local x,y,z = inst.Transform:GetWorldPosition()                                                                                                  --
local ents = TheSim:FindEntities(x, y, z, 20, { "_combat" }, { "player", "shadow", "shadowminion" })                                             --
for i,v in ipairs(ents) do                                                                                                                       --
if v:HasTag("tree") then                                                                                           --                                                                                                                                           --
v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --
v:DoTaskInTime(0.44, function(v) v.AnimState:PlayAnimation("chop") end)                                                              --                                                              --
end                                                                                                                                              --
end                                                                                                                                              --

So how would I do that? It's okay if it's not possible then please tell me then I can delete this thread because I feel ashamed to put up so many threads all asking for help :?...

And thanks for reading my problem I hope you have a wonderful day/night :D!!!!

 

PS: If someone also know how would I get this to work? I'm trying to also make spider houses look like they're being hit so I went in the spiderden.lua for when it gets hit & saw this "inst.AnimState:PlayAnimation(inst.anims.hit)" but it just crashes the game why?!


local x,y,z = inst.Transform:GetWorldPosition()                                                                                                  --
local ents = TheSim:FindEntities(x, y, z, 20, { "_combat" }, { "player", "shadow", "shadowminion" })                                             --
for i,v in ipairs(ents) do                                                                                                                       --
if v:HasTag("spiderden") then                                                                                           --
if v.components.health:IsDead() then                                                                                                             --
return                                                                                                                                           --
end                                                                                                                                              --
v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)                                                              --           
end                                                                                                                                              --
end                                                                                                                                              --

 

Your current FindEntities looks for entities with a '_combat' tag, which trees most probably don't have.

That's why you can't find them.

Link to comment
Share on other sites

3 hours ago, PanAzej said:

Your current FindEntities looks for entities with a '_combat' tag, which trees most probably don't have.

That's why you can't find them.

I removed combat & nothing happens but that's fine thanks anyways :D!

Link to comment
Share on other sites

You can use the "CHOP_workable" tag.

And trees are in fact entities, like almost everything in the game. Hell, even the world itself is an entity.

So your FindEntities call becomes something like this:

local ents = TheSim:FindEntities(x, y, z, 20, { "CHOP_workable" }, { "player", "shadow", "shadowminion"})

 

Edited by Joachim
Link to comment
Share on other sites

And the reason the game crashes when you try to shake the spiderdens is probably because you tried to access a non-existing variable. Why?

This is your code:

v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)

You are inside a for-loop, iterating over all spiderdens. The spiderden is assigned to the variable v. It is the spiderden that contains the reference to the hit animation. You, however, try to access the hit animation from the perspective of the observer (i.e., inst), which is not a spiderden and therefore does not have this variable set.

So, to fix this, you replace this by:

v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(v.anims.hit) end)

And that should work, I guess.

Link to comment
Share on other sites

Advice for your future modding efforts: check your client_log for errors when the game crashes. You could have figured it out yourself this way. Reading the client_log is essential when you are modding. I almost always keep it open while I am working on something, because I know that new problems will arise and this way I can determine the cause of the problem, which helps me to fix it.

Edited by Joachim
Link to comment
Share on other sites

@JoachimThanks i'm really close if you can just help me if you know how to one more time sorry :D! So, I put code like this for trees

local x,y,z = inst.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 20, { "CHOP_workable" }, { "player", "leif", "shadow", "shadowminion" })                                      
for i,v in ipairs(ents) do                                                                                           
if v:HasTag("tree") then                           
v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation("chop") end) 
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation("chop") end) 
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation("chop") end)
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation("chop") end)
v:DoTaskInTime(0.44, function(v) v.AnimState:PlayAnimation("chop") end)
end                          
end

And the code works but the animation doesn't play & it just makes all the trees invisible? Can you tell me what i'm doing wrong thanks :D!

Also!

40 minutes ago, Joachim said:

 

And the reason the game crashes when you try to shake the spiderdens is probably because you tried to access a non-existing variable. Why?

This is your code:


v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(inst.anims.hit) end)

 

 

40 minutes ago, Joachim said:

So, to fix this, you replace this by:


v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(v.anims.hit) end)

The code you said to fix the spiderdens is the same as my code that isn't working. xD

PS: Thanks you a lot for your help I really appreciate it :D!

Link to comment
Share on other sites

It is because the chop animation is different from the one you are using. Use this instead:

v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
Quote

The code you said to fix the spiderdens is the same as my code that isn't working. xD

It is not the same code. Did you use it at all?

Link to comment
Share on other sites

Btw, unrelated to your problem, I would not repeat the same code more than once. Use a loop instead:

for k=0,4 do
	v:DoTaskInTime(0.01 + k * 0.11, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
end

Edit: Replaced i by k in this loop, to avoid any problems if you were to copy and paste it.

Edited by Joachim
Link to comment
Share on other sites

Thanks so much Joachim the trees now shake fine, really thanks a lot I couldn't have done it without you :D!!!! Also, you're right I didn't use the spiderden code because I thought it was the same code but I didn't see it's actually different sorry i'm gonna try use it now!! xD

And sorry if I ask but why would you not repeat the same code more than once? Does it cause more lag or something because that wouldn't be good & lots of my stuff I use repeat code because i'm not very smart... Dx

Again thanks so much :D!!!!!

 

PS: SO SORRY but if I can ask since you say repeat code's not good could you make this repeat code into the loop thingy, very sorry & you don't have to :)!!! If you do this I can hopefully learn to put the code you put instead, sorry for being such a bother!!!!!!!

v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.44, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.55, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.66, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.77, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.88, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.99, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.11, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.21, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.31, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.41, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.51, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.57, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)

 

Edited by SuperDavid
Link to comment
Share on other sites

Happy to help.

The reason why it is better to not repeat the same code more than once is to make it easier to change and re-use. For example, you could simply change the 4 in the for-loop to an 8, and you could have the trees shake even longer. If you were to repeat the code, you would have to write (or copy and edit) 4 additional lines of code while you only needed to change a 4 to an 8 in the loop version.

You don't have to be super smart to generalize code. It is easy to spot. When you see multiple lines of code that more or less do the same thing, then there is probably room to generalize it. You just have to recognize the pattern. It requires some practice, but it gets better over time.

Edited by Joachim
Link to comment
Share on other sites

v:DoTaskInTime(0.01, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
v:DoTaskInTime(0.11, function(v) v.AnimState:PlayAnimation(v.anims.chop) end) 
v:DoTaskInTime(0.22, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.33, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.44, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.55, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.66, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.77, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.88, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(0.99, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.11, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.21, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.31, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.41, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.51, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
v:DoTaskInTime(1.57, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)

I am going to assume that the specific time intervals do not matter that much, so I am going to change the intervals a bit, so that they become linear. If I can assume that each interval happens 0.11 seconds after the previous one, then you can express this as f(t) = 0.11 * t + 0.01. You can ignore this formula if you don't like math, although it should be easy to understand. When t = 0, that is the first shake. When t = 1, that is the second time the tree shakes. And so on.

So we get:

for k=0,14 do
	v:DoTaskInTime(0.01 + 0.11 * k, function(v) v.AnimState:PlayAnimation(v.anims.chop) end)
end

You understand what this does right? It means that you go from k = 0 to k = 14 (including 14), which amounts to 15 steps (since 0 also counts). Every time, the code within the body of the for-loop gets called and k assumes one of these values between 0 and 14. So you can multiply it with 0.11, and add 0.01, to get the intervals that you want (more or less).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...