Using body:destroy() without the game crashing

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
war_man333
Prole
Posts: 6
Joined: Tue May 27, 2014 5:20 pm
Location: Aarhus, Denmark

Using body:destroy() without the game crashing

Post 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? :awesome:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Using body:destroy() without the game crashing

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
war_man333
Prole
Posts: 6
Joined: Tue May 27, 2014 5:20 pm
Location: Aarhus, Denmark

Re: Using body:destroy() without the game crashing

Post 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.
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: Using body:destroy() without the game crashing

Post 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?
User avatar
war_man333
Prole
Posts: 6
Joined: Tue May 27, 2014 5:20 pm
Location: Aarhus, Denmark

Re: Using body:destroy() without the game crashing

Post 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
Crash-error
error.PNG (94.37 KiB) Viewed 6458 times
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: Using body:destroy() without the game crashing

Post 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.
User avatar
war_man333
Prole
Posts: 6
Joined: Tue May 27, 2014 5:20 pm
Location: Aarhus, Denmark

Re: Using body:destroy() without the game crashing

Post by war_man333 »

Here's the code:
game.rar
(452.54 KiB) Downloaded 293 times
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: Using body:destroy() without the game crashing

Post 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...
User avatar
war_man333
Prole
Posts: 6
Joined: Tue May 27, 2014 5:20 pm
Location: Aarhus, Denmark

Re: Using body:destroy() without the game crashing

Post 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...
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: Using body:destroy() without the game crashing

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 2 guests