Page 1 of 1

Bullets Disappearing when removing them during ipairs loop.

Posted: Sat May 20, 2017 8:44 pm
by demvive
Hello! I'm having an issue that you may be able to notice with this file
SID.love
(51.57 KiB) Downloaded 100 times
. When bullets are colliding with minions or enemies, sometimes but not every time there are extra bullets off to the side that are removed which shouldn't be.

The relevant line of code is likely line 30 of bullet.lua. That's where the collision check and table.remove are. Other than that, I have no idea.

Re: Bullets Disappearing when removing them during ipairs loop.

Posted: Sat May 20, 2017 9:04 pm
by raidho36
Long story short, you can't modify the table you're iterating over. It won't work properly. Your can attempt traversing the table manually in controlled fashion. Or use alternative method that doesn't involves immediately removing items from the table.

Re: Bullets Disappearing when removing them during ipairs loop.

Posted: Sat May 20, 2017 9:16 pm
by davisdude
What you need to do is loop over the table backwards. This is because the length of the table changes over the course of iteration, so you can't use it as the ending point.

Code: Select all

for i = #tab, 1, -1 do
    -- code
end