## Damage effect show problem!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
tetsuken
Prole
Posts: 13
Joined: Sat May 25, 2013 10:42 pm

### Damage effect show problem!

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.

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

### Re: Damage effect show problem!

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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end

tetsuken
Prole
Posts: 13
Joined: Sat May 25, 2013 10:42 pm

### Re: Damage effect show problem!

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
Last edited by tetsuken on Sat Aug 08, 2015 8:25 pm, edited 1 time in total.

arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

### Re: Damage effect show problem!

Maybe not use table.remove but effectlist = nil & pairs/ipairs loop?

TurtleP
Party member
Posts: 147
Joined: Thu Mar 22, 2012 9:20 pm
Contact:

### Re: Damage effect show problem!

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!

tetsuken
Prole
Posts: 13
Joined: Sat May 25, 2013 10:42 pm

### Re: Damage effect show problem!

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

### Who is online

Users browsing this forum: No registered users and 32 guests