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.mModel2 = loader.loadModel("models/explosion/explosion1/smallExplosion.egg")
self.mNode2 = self.mModel2.find('**/+SequenceNode')
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?
You need to remove, or detach, the node when you are done with it. Something like this:
taskMgr.doMethodLater(self.delayTime, self.cleanExplosion, self.taskName)
def cleanExplosion(self, task):
self.delayTime should be the length of your sequence in seconds, and self.taskName should be a unique string for each instance of self.
Note that you might also do this with an Interval instead of with a doMethodLater, and that might be easier to understand and manage.
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?
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.
I will have a look at this!