Question about loops
Posted: Tue Aug 20, 2019 2:08 am
Hi!, i've been struggling with the differences or not between this two ways of doing this.
From my beginner point of view, it's the same but maybe there are potencial bugs coming once the proyect moves forward so that's why i'm asking:
and the other way i was doing it was:
is it really the same? is one way better than the other? both are wrong? any suggestions?
Thanks in advance!
From my beginner point of view, it's the same but maybe there are potencial bugs coming once the proyect moves forward so that's why i'm asking:
Code: Select all
for i, m in ipairs(monsters) do
for j, b in ipairs(bullets) do
if check_collision(b.x, b.y, b.width, b.height, m.x, m.y, m.width, m.height) then
table.remove(bullets, j)
m.hp = m.hp - b.damage
if m.hp == 0 then
table.remove(monsters, i)
end
end
end
end
Code: Select all
for i, m in ipairs(monsters) do
for j, b in ipairs(bullets) do
if check_collision(b.x, b.y, b.width, b.height, m.x, m.y, m.width, m.height) then
b.dead = true
m.hp = m.hp - b.damage
if m.hp == 0 then
m.dead = true
end
end
end
end
for i=#monsters,1,-1 do
local m = monsters[i]
if m.dead == true then
table.remove(monsters, i)
end
end
for i=#bullets, 1, -1 do
local b = bullets[i]
if b.dead == true then
table.remove(bullets, i)
end
end
Thanks in advance!