deleting table entries in a loop and world:getBodyCount

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
knorke
Party member
Posts: 240
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

deleting table entries in a loop and world:getBodyCount

Post by knorke »

Ok I have a little problem with my game (http://love2d.org/forums/viewtopic.php?f=5&t=1716)
When I delete a bullet or rock their ghosts seem to be still floating around and the ships bounce away from empty places. :huh:
I am aware you can not simply use table.remove or = nil while looping through a table because it fücks up indezes, getn() etc.
So how can this be solved?

In a new version I mark bullets for deleting and then do that in a second loop, like this:

Code: Select all

local bullet_delete_count = 0
	for i,v in ipairs(bullets) do
		local xs, ys = v.b:getLinearVelocity()
--check for various reasons a bullet might need to be removed
		if ((xs*xs) + (ys*ys) < bullet_min_speed or v.status=="HIT" or is_offscreen (v.b:getX(), v.b:getY()) == true) then  --status = "HIT" is set in collision callin after colliding with a rock
		bullet_delete_count = bullet_delete_count +1
--table.remove (bullets, i)   <--would break stuff
		v.status = "DELETE"
		end
		end
	---delete loop for bullets----
for bla = 1, bullet_delete_count, 1 do
	for i,v in ipairs(bullets) do	
		if (v.status == "DELETE") then 
			table.remove (bullets, i) 
			v.b:destroy() --adding or remove
			v.s = nil  -- this 2 lines has no effet
			bullet_delete_count = bullet_delete_count - 1 
			break 
			end
		end
	end
Much hackish yes?
This partly works (bullets are removed from game) but the "invisible ghost" problem remains. Does this technic maybe create holes in the array/memory leaks/whatever that can cause that?

Also I noticed that getBodyCount() does not decrease after a bullet gets deleted.

This seems to be true for all kind of bodies?
Test this small example:

Code: Select all

local bodies = {}

function love.load()
world = love.physics.newWorld(2000, 2000)
end

function love.update(dt)
if love.keyboard.isDown("a") then 
	local new_b = love.physics.newBody(world, 100, 100)
	table.insert (bodies,new_b)
	end
if love.keyboard.isDown("d") then 
	table.remove (bodies)
	end
end

function love.draw()
love.graphics.print("#bodies:" .. #bodies, 200, 210)
love.graphics.print("world:getBodyCount():" .. world:getBodyCount(), 200, 230)
end
Press a to add bodies and d to delete bodies. Notice how #bodies goes down then deleting bodies but world:getBodyCount() only goes upwards.
wiki says "Get the number of bodies in the world." I read that as "number of bodies currently active in this world"
Does it mean "number of bodies that once very active in this world"?
Or more likely, what am I doing wrong with deleting the body?

Oh the text got very long now. I hope somebody understands the problem and can help :)


edit

Code: Select all

v.b:setX(-666) v.b:setY(-666) --get the fuck away from me
v.b:setLinearVelocity (0,0)	--stay the fuck away from me
ok, this seems to "solve" it (must test more) but I am still interessted in the BodyCount thing.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: deleting table entries in a loop and world:getBodyCount

Post by Robin »

I'm not sure, but I think you can solve this by having the delete loop like this: (warning, pseudocode ahead)

Code: Select all

for i = #bullets, 1, -1 do
    if bullets[i].remove then
         table.remove(bullets, i)
    end
end
Help us help you: attach a .love.
User avatar
knorke
Party member
Posts: 240
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: deleting table entries in a loop and world:getBodyCount

Post by knorke »

counting backwards? worth a try, thanks.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: deleting table entries in a loop and world:getBodyCount

Post by kikito »

The backwards loop is a very nice trick. Thanks!
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: slime and 57 guests