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
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

body:destroy()

Post by tentus »

If my reading of Box2D is right, destroy()ing a body also, by necessity, destroys all shapes associated with it, right? I recently discovered that niling/refilling a table a body was in does not delete the body, so I want to try and find out if there are any other oddities I should know about.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: body:destroy()

Post by Robin »

I don't think you ever need to call body:destroy() yourself. You have to have zero references to it though.
Help us help you: attach a .love.
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 »

Robin wrote:I don't think you ever need to call body:destroy() yourself. You have to have zero references to it though.
I'm not so sure about that. If you have zero references and you manually invoke the garbage collector, then you are probably safe. Otherwise, I think there could be some "phantom bodies" laying around until the GC is launched. I don't have time to make the test right now.
When I write def I mean function.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: body:destroy()

Post by tentus »

I guarantee the GC won't get it, unless the GC takes more than 30 seconds to work (in a test scenario that runs at 400-600 fps on my computer). I think that because bodies are inside the world, the GC thinks they're not garbage, which makes sense. Regardless, I can't have phantom player bodies sitting around after every death, or worse, breakable blocks that don't actually break, enemies that won't die, or explosions that never stop being dangerous.

Remember, I can nil out the table the bodies are in completely, but they stick around.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: body:destroy()

Post by Robin »

That is odd.
Help us help you: attach a .love.
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 »

I've tried in vain to destroy bodies after creation, but I can't. If I use body:destroy(), usually the result is a big crash a few seconds later with a weird Box2D error. Other times, there will be ghost bodies lying around for a few seconds afterwards, and then after a little bit the ghost bodies will go away... but usually not long after another crash occurs.

So in short, I can't destroy bodies.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: body:destroy()

Post by tentus »

I'm having some success by simply moving bodies to above the world, where the player can't get to, and then calling destroy. I dunno if destroy is working (I suspect not, since if I don't move the objects I can expect crashes like BlackBullet described), and this method worries me because I'm shooting in the dark.

Could we go ahead and request the feature that destroy() return a boolean, true if it was a success, and false if not?
Kurosuke needs beta testers
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 »

Well that's a way of pretending they're not there at least, either moving them outside the world or putting them on a blank collision layer. But yeah, I don't think destroy() works properly (not for me anyway).
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 »

Alright, let's just end all this:
For both destroy() and the GC to function, all shapes, contacts and other associated things have to be released as well.
Then, the difference between destroy() and the GC: destroy() is there to force removal at that specific time, the GC may take some longer.
As for the crashes: If you destroy an object that is still colliding Box2D won't like it, which is why moving it outside of the world, or setting it to no longer collide solves this problem.
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 »

Ah, that helps, thanks. Also explains the unpredictability of the crashes.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest