Why is table.remove making my program crash?

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.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Why is table.remove making my program crash?

Post by miko »

Taehl wrote:
miko wrote:So it was real 2-D problem, which can not (easily) be "linearized", like your case (in which there are no interactions between mobs and ents elements).
Argh, fine!

Code: Select all

for i=math.max(#mobs, #ents), 1, -1 do
	local m,isDead = mobs[i]
	for i=#ents, 1, -1 do
		local e = ents[i]
		if whateverItIsYoureDoing then
			isDead = true
			table.remove(ents, i)
		end
	end
	if isDead then table.remove(mobs, i) end
end
The same basic method I presented still applies.
Not in this case - you have somewhat complicated the code, but still did not solve the problem ;) Note that your magical whateverItIsYoureDoing() function could remove elements from mobs and/or ents table at the index lower than the current value of i, which would break your iteration.

Try to change the code cited by ivan here:
http://love2d.org/forums/viewtopic.php? ... 841#p38800

Just remember that you need to check collisions between PlayerBullets[1] and Enemies[N], and also between PlayerBullets[M] and Enemies[1] (and every combination in between).
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Why is table.remove making my program crash?

Post by Taehl »

Well, Don't Do That! The whateverItIsYoureDoing function represents checking collision or whatever to see if a pair of things need to be removed. That you can stick arbitrary code designed to mess up the example into a placeholder is a given.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Why is table.remove making my program crash?

Post by miko »

Taehl wrote:Well, Don't Do That! The whateverItIsYoureDoing function represents checking collision or whatever to see if a pair of things need to be removed. That you can stick arbitrary code designed to mess up the example into a placeholder is a given.
And that was my point. The original code was "designed" just to "mess up", or in other words - you cannot easily do the same logic in one pass with one loop. Or if you can - just try to write it.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Why is table.remove making my program crash?

Post by Taehl »

I /do/ write it like that. As I said, that's my normal method.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

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