Explosion!!!

Hi everyone,

It’s me again (2 messages in the same day…). I’m currently trying to correct some bug in my game and this one drive me crazy!

Each time a collision is detected from a weapon to a enemy, an explosion is trigered. This is working. The problem is that all the explosion STAY in the scene graph after the animation is finished.

Here is the code:

def playSmallExplosion(self, pos, hpr):
        self.orbit_explosion2 = self.world.attachNode()
        self.orbit_explosion2.setHpr(hpr)
        
        self.mModel2 = loader.loadModel("models/explosion/explosion1/smallExplosion.egg") 
        self.mModel2.setScale(2,2,2) 
        self.mModel2.setBillboardPointEye() 
        self.mModel2.setLightOff()
        self.mModel2.show()
        self.mNode2 = self.mModel2.find('**/+SequenceNode') 
        self.mNode2.node().stop() 
        self.mNode2.node().pose(0)
        self.mModel2.reparentTo(self.orbit_explosion2)
        self.mModel2.setPos(pos) 
        
        self.mNode2.node().play() 

Each time this method is called, all of this is called. This is working but I’m sure I will get trouble soon or later with memory. Also, I can see the explosion last frame when I go back where it happened.

Do someone have an idea for me?
Thanks!

Jaff

Nobody??? :frowning:

You need to remove, or detach, the node when you are done with it. Something like this:

        self.mModel2.reparentTo(self.orbit_explosion2)
        self.mModel2.setPos(pos)
       
        self.mNode2.node().play() 
        taskMgr.doMethodLater(self.delayTime, self.cleanExplosion, self.taskName)

    def cleanExplosion(self, task):
        self.mModel2.detachNode()

self.delayTime should be the length of your sequence in seconds, and self.taskName should be a unique string for each instance of self.

David

Note that you might also do this with an Interval instead of with a doMethodLater, and that might be easier to understand and manage.

David

Hi,

Thanks for the answer. I tried something like that but there was another problem involved I don’t remember what.

But today I got a flash and I will change the way I manage my explosion. I will have an ExplosionManager that will contain a list of current explosion. Each explosion will have a time to live and will be deleted right after. It maybe not the best way to do it but it’s working with my weapon class.

Just 2 questions to check if this method is good:
-Is there a way in Python to display all class that are currently instantiated. (My node will be deleted but it’s not bether if I have like 4000 object “Explosion” created, I want to check that)
-Is there a way to clearly kill an object and that way the garbage collector will free the memory?

Thanks!

Jaff

Python’s “gc” module can do this sort of thing for you. You’ll have to look into how it is used.

Simply removing all references to the object is the way you kill something in Python. You’re not supposed to even have to worry about questions like this; it’s just supposed to happen automatically.

David

Nice thanks

I will have a look at this!

Jaff