body:destroy()

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
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: body:destroy()

Post by BlackBulletIV »

Oh.... ok. Thanks for that.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: body:destroy()

Post by bartbes »

I remember rude saying something about destroy waiting a frame...
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: body:destroy()

Post by kikito »

Can we just make body:destroy() "mark it for destroying" ( + mask stuff) and then removing the body on the next frame?
When I write def I mean function.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: body:destroy()

Post 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.
User avatar
lizard
Prole
Posts: 16
Joined: Wed Aug 04, 2010 6:38 pm
Location: Thailand

Re: body:destroy()

Post 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)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: body:destroy()

Post 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.
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: body:destroy()

Post by bartbes »

And I was trying to say I heard rude proclaiming several times that that is the case.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: body:destroy()

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: body:destroy()

Post by bartbes »

I only know what rude told me.. anyway it does wait 1 gc cycle.. :P
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], rabbitboots and 7 guests