Page 3 of 3

Re: body:destroy()

Posted: Wed Mar 02, 2011 10:20 am
by BlackBulletIV
Oh.... ok. Thanks for that.

Re: body:destroy()

Posted: Wed Mar 02, 2011 11:23 am
by bartbes
I remember rude saying something about destroy waiting a frame...

Re: body:destroy()

Posted: Wed Mar 02, 2011 4:17 pm
by kikito
Can we just make body:destroy() "mark it for destroying" ( + mask stuff) and then removing the body on the next frame?

Re: body:destroy()

Posted: Wed Mar 02, 2011 8:49 pm
by BlackBulletIV
So if it waits a frame, then maybe I don't have to wait. I guess the way to find out is to test it.

Re: body:destroy()

Posted: Fri Mar 04, 2011 4:21 pm
by lizard
Not necessarily wait for the next frame. You can mark a entity to remove, update the world, and delete entity.
I'm using the following method:

Code: Select all

function Unit:predelete()
    -- remove shapes from contacts (important for collision callbacks)
    for _, shape in pairs(self.shapes) do
        shape:setMask(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
    end
    self.predeleted = true
end

function Unit:delete()
    if self.predeleted then
        for i, joint in pairs(self.joints) do
            joint:destroy()
        end
        self.joints = {}
        for i, shape in pairs(self.shapes) do
            shape:destroy()
        end
        self.shapes = {}
        for i, body in pairs(self.bodies) do
            body:destroy()
        end
        self.bodies = {}
        self.deleted = true
    end
end

function love.update()
    if not paused then
        -- prepare for removing (remove collision contacts)
        for _,unit in ipairs(for_removal) do
            unit:predelete()
        end
        -- update all
        for _,unit in ipairs(units) do unit:update() end
        world:update(dt)
        -- remove
        if for_removal ~= {} then
            for _,unit in ipairs(for_removal) do
                unit:delete()
            end
            for_removal = {}
        end
    end
end
And somewhere:

Code: Select all

table.insert(for_removal, some_unit)

Re: body:destroy()

Posted: Fri Mar 04, 2011 5:19 pm
by kikito
That is what I meant when I said that löve should do it. I meant that it should mark it for deletion, unmask it and automatically remove it on the next physics update.

Re: body:destroy()

Posted: Fri Mar 04, 2011 5:40 pm
by bartbes
And I was trying to say I heard rude proclaiming several times that that is the case.

Re: body:destroy()

Posted: Sat Mar 05, 2011 9:27 am
by BlackBulletIV
Here's the code for the destroy function:

Code: Select all

int w_Body_destroy(lua_State * L)
{
	Proxy * p = (Proxy *)lua_touserdata(L, 1);
	p->own = false;

	Body * t = (Body *)p->data;
	t->release();

	return 0;
}
And the code for the Body's destructor:

Code: Select all

Body::~Body()
{
	world->world->DestroyBody(body);
	world->release();
	body = 0;
}
I don't see anything about waiting a frame. Am I missing something?

Re: body:destroy()

Posted: Sat Mar 05, 2011 11:22 am
by bartbes
I only know what rude told me.. anyway it does wait 1 gc cycle.. :P