Page 1 of 2
Using body:destroy() without the game crashing
Posted: Tue May 27, 2014 5:49 pm
by war_man333
I'm trying to remove a body, but I get an error for using the body after I have removed it.
Code: Select all
function Projectile:onCollisionEnter(object, contact)
self.body:destroy()
end
There's still references(?) to the body, specifically it keeps running 'self:physicsBodyDraw'.
Sorry for being clueless and new, but how do I fix this?

Re: Using body:destroy() without the game crashing
Posted: Tue May 27, 2014 8:41 pm
by davisdude
Hi, welcome to the community!
Sorry, but we can't help you without more information: a .love would help.
As for my guess, you're trying to access it after you've destroyed it. This is like trying to access a variable you're already "nilled". You need to check whether the variable still exists before you access it.
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 4:43 pm
by war_man333
davisdude wrote:Hi, welcome to the community!
Sorry, but we can't help you without more information: a .love would help.
As for my guess, you're trying to access it after you've destroyed it. This is like trying to access a variable you're already "nilled". You need to check whether the variable still exists before you access it.
What's a .love?
Entire projectile.lua
Code: Select all
Projectile = mg.class('Projectile', mg.Entity) -- Name of class. Type = entity
Projectile:include(mg.PhysicsBody) -- Bitch got physics
Projectile.static.enter = {'Solid'}
function Projectile:init(world, x, y, settings, bdirection)
mg.Entity.init(self, world, x, y, settings)
self:physicsBodyInit(world, x, y, settings)
self.animation_state = 'idle'
bd = bdirection
self.idle = mg.Animation(love.graphics.newImage('resources/particles/bullet.png'), 6,6,0)
if true then
self.body:setLinearVelocity(1000, -100)
end
--if then
-- self.body:setLinearVelocity(-1000, -100)
--end
end
function Projectile:onCollisionEnter(object, contact)
end
function Projectile:draw()
self:physicsBodyDraw()
self[self.animation_state]:draw(
self.x - self[self.animation_state].frame_width/2,
self.y - self[self.animation_state].frame_height/2 - 2)
end
There should be some sort of code for destroying the projectile in 'Projectile:onCollisionEnter'
I'm using mogamett by the way.
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 5:31 pm
by ArchAngel075
a .love : read here for what it is and how it helps you, and helps us >
http://www.love2d.org/wiki/Game_Distribution
Else provide a zip or rar of the game, it is -somewhat- the same thing if you aren't nitpicky
Are you by chance -not- removing any references to the projectile object as-well when destroying it?
In the past, i found using a centralized Database storing all my OOP objects etc to be the best method when dealing with references.
Each object is stored as a member in the Database table whith its Unique ID(UID) as its key, then whenever I operate on a object i can use getObject(UID) which returns the object. The reason for this is if I pass the UID around and not the object itself i only have at most two references of the object at a time, thus setting it to nil in the database clears all references.
anyhow, the point made is : is the Projectile object also destroyed and any attempt to draw it stopped(as it shouldnt exist anymore) or is there perhaps something somewhere still calling the function that draws the object?
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 6:23 pm
by war_man333
ArchAngel075 wrote:
anyhow, the point made is : is the Projectile object also destroyed and any attempt to draw it stopped(as it shouldnt exist anymore) or is there perhaps something somewhere still calling the function that draws the object?
I don't know how to destroy the object, I just want the entire object, body, everything - removed from the game when it hits a wall.
I'm not an expert with OOP, I've only been programming a small amount of Java and some Windows Forms applications (C#).
Anyway, here's the error.

- Crash-error
- error.PNG (94.37 KiB) Viewed 6496 times
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 7:04 pm
by ArchAngel075
Well, wherever you create the projectile object, its reference - if set to nil should destroy it (though thats only if its the only reference)
also, in your draw code at the top(or above the line of the code that crashes) add
Code: Select all
if not theBody then print("Body is null") ; return end
well replace theBody with the reference to the objects body, the code should print "Body is null" and prevent the code below it from executing if the body is nil(destroyed)
Perhaps, as said before, you can provide a .love so that forum users can troubleshoot with the actual code than assume and contemplate.
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 7:15 pm
by war_man333
Here's the code:
- game.rar
- (452.54 KiB) Downloaded 295 times
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 8:19 pm
by ArchAngel075
right...
Im having trouble following the code, never worked with mogamett before. To help direct me, tell me the file, line number(if possible) and line of code(s) you change to cause the body to be destroyed, also where do you store the reference to the projectiles upon their creation? ie, do you use a single database or a player.projectiles table etc. If i know where this is i know what to nil more or less on body:destroy()
its just so much files to sift through haha...
Re: Using body:destroy() without the game crashing
Posted: Wed May 28, 2014 8:43 pm
by war_man333
ArchAngel075 wrote:right...
Im having trouble following the code, never worked with mogamett before. To help direct me, tell me the file, line number(if possible) and line of code(s) you change to cause the body to be destroyed, also where do you store the reference to the projectiles upon their creation? ie, do you use a single database or a player.projectiles table etc. If i know where this is i know what to nil more or less on body:destroy()
its just so much files to sift through haha...
I'm not using anything to store the projectiles. I guess that's the problem, I don't have an identifier on any of the projectiles.
I remove the comment (--) on this function here, and it crashes:
Code: Select all
function Projectile:onCollisionEnter(object, contact)
--self.body:destroy()
end
On a side note, do you think it's possible to get attributes from my Player class somehow? I need it to set which direction the bullets should fly...
Re: Using body:destroy() without the game crashing
Posted: Thu May 29, 2014 12:20 am
by adnzzzzZ
If you're using Mogamett, do self.dead = true and the physics object will be destroyed next frame. You can't change a physics object from the callback function.
If you have more questions like those use the comments section on the related page on the Mogamett site, since I get an e-mail notification right away and can answer the question pretty fast.