Page 2 of 2

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

Posted: Fri Oct 28, 2011 9:50 pm
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).

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

Posted: Fri Oct 28, 2011 10:13 pm
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.

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

Posted: Fri Oct 28, 2011 10:37 pm
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.

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

Posted: Fri Oct 28, 2011 11:19 pm
by Taehl
I /do/ write it like that. As I said, that's my normal method.