Bullets Disappearing when removing them during ipairs loop.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
demvive
Prole
Posts: 3
Joined: Tue Jan 31, 2017 12:18 am

Bullets Disappearing when removing them during ipairs loop.

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Bullets Disappearing when removing them during ipairs loop.

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Bullets Disappearing when removing them during ipairs loop.

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 47 guests