Page 1 of 1
Damage effect show problem!
Posted: Sat Aug 08, 2015 7:07 am
by tetsuken
hello guys, i have a problem and i hope you can help me with it.
I made a function to show damage above the enemies but its not working properly.
here is the code with problem:
Code: Select all
function EffectUpdate(t)
if effectlist ~= nil then
for i=1,#effectlist do
effectlist[i].time = effectlist[i].time + t*400 -- problem is showing on this line
effectlist[i].y = effectlist[i].y - 1
if effectlist[i].time > effectlist[i].duration then
table.remove(effectlist, i)
end
end
end
end
description of the problem is this: if i attack the enemy fast the second or third damage gives a index nil problem.
I have no clue where to start solve this if someone can help i will appreciate.
Re: Damage effect show problem!
Posted: Sat Aug 08, 2015 10:48 am
by s-ol
You need to iterate the table backwards, when you table.remove the first effect but there were two effects then the second one gets moved to the first slot but you still enter the loop again with i=2.
Re: Damage effect show problem!
Posted: Sat Aug 08, 2015 7:29 pm
by tetsuken
I'm not to good on programming could give me an example? how to iterate backward?
i try like this :
Code: Select all
function EffectUpdate(t)
if effectlist ~= nil then
for i = #effectlist, 1, -1 do
if effectlist[i].time > effectlist[i].duration then -- problem here index nil continues
table.remove(effectlist, i)
else
effectlist[i].time = effectlist[i].time + t*400
effectlist[i].y = effectlist[i].y - 1
end
end
end
end
but the problem persists in te same line
Re: Damage effect show problem!
Posted: Sat Aug 08, 2015 8:05 pm
by arampl
Maybe not use table.remove but effectlist = nil & pairs/ipairs loop?
Re: Damage effect show problem!
Posted: Sat Aug 08, 2015 8:06 pm
by TurtleP
Tetsuken, try the following:
Code: Select all
function EffectUpdate(t)
if effectlist ~= nil then
for i = #effectlist, 1, -1 do
if effectlist[i] then
if effectlist[i].time > effectlist[i].duration then -- problem here index nil continues
table.remove(effectlist, i)
else
effectlist[i].time = effectlist[i].time + t*400
effectlist[i].y = effectlist[i].y - 1
end
end
end
end
end
You should be iterating backwards through the effectlist table since that's what you want to remove. Secondly, it's going to crash if it's trying to index a value which doesn't exist, hence why I suggest putting "if effectlist
then".
Hope this helps!
Re: Damage effect show problem!
Posted: Sat Aug 08, 2015 8:30 pm
by tetsuken
ty that same works perfectly
i had found the mistake on the first attempt of inverse table remove, but your sample also work fine.
actually i think is better then mine
Code: Select all
if effectlist ~= nil then
for i = #effectlist, 1, -1 do -- i was make worng here using entitylist instead effectlist
if effectlist[i].time > effectlist[i].duration then
table.remove(effectlist, i)
else
effectlist[i].time = effectlist[i].time + t*400
effectlist[i].y = effectlist[i].y - 1
end
end
end
ty very much