Game Development Community

Unload Particles on exit

by Neil Marshall · in Torque Game Engine · 05/02/2006 (12:14 pm) · 8 replies

I needed the particles to unload on exit with the browser plugin as they were causing a crash and I noticed a few other people having related problems so I figured I'd post my code (TomS asked in my plan actually).

game\fx\particleEmitter.cc
IMPLEMENT_CO_DATABLOCK_V1(ParticleEmitterNodeData);
IMPLEMENT_CO_NETOBJECT_V1(ParticleEmitterNode);

[b]Vector<ParticleEmitter*> ParticleEmitter::smActiveEmitters; [/b]

//--------------------------------------------------------------------------
//--------------------------------------
//
ParticleEmitterNodeData::ParticleEmitterNodeData()

game\fx\particleEngine.h
ParticleEmitter();
   ~ParticleEmitter();

[b]   static Vector<ParticleEmitter*> smActiveEmitters;
   static void removeAllEmitters();[/b]

   /// Sets sizes of particles based on sizelist provided
   /// @param   sizeList   List of sizes
   void setSizes( F32 *sizeList );

game\fx\particleEngine.cc
mLastPosition.set(0, 0, 0);
   mHasLastPosition = false;

   mLifetimeMS = 0;
   mElapsedTimeMS = 0;

[b]   smActiveEmitters.push_back(this);[/b]
}

ParticleEmitter::~ParticleEmitter()
{
   AssertFatal(mParticleListHead == NULL, "Error, particles remain in emitter after remove?");

[b]   Vector<ParticleEmitter*>::iterator itr;
   for (itr = smActiveEmitters.begin(); itr != smActiveEmitters.end(); itr++)
   {
	   if ((*itr) == this) {
         Con::printf("ParticleEmitter freed"); 
		   smActiveEmitters.erase(itr);
		   break;
   	}
   }[/b]
}

Parent::onRemove();
}

[b]// Function to clear all emitters
void ParticleEmitter::removeAllEmitters()
{
	Con::printf("Somebody left %d ParticleEmitter's on!", smActiveEmitters.size());
	Vector<ParticleEmitter*>::iterator itr = smActiveEmitters.begin();
 
	while (smActiveEmitters.size() != 0)
   {
		// Delete object
		smActiveEmitters[0]->unregisterObject();
		delete smActiveEmitters[0];
	}
   smActiveEmitters.clear();
}[/b]

bool ParticleEmitter::onNewDataBlock(GameBaseData* dptr)

void destroy()
{
   AssertFatal(sgParticleEngine != NULL, "ParticleEngine::destroy: engine not initialized");

[b]   ParticleEmitter::removeAllEmitters();[/b]

   delete sgParticleEngine;
   sgParticleEngine = NULL;
}

} // namespace ParticleEngine

#1
05/02/2006 (1:04 pm)
Please post as a "real" resource. Else it will most likely drown in all the forum posts :-)

Good one BTW. Should always clean up
#2
05/02/2006 (2:43 pm)
This is nice one Neil!
works fine with TLK.
And yeah, it's need's to be posted as resource or even in HEAD.
#3
05/02/2006 (4:06 pm)
Nice one, I can remove my try-catch hack now ;-)
#4
05/03/2006 (12:52 am)
Well hopefully we can get this fix into HEAD... Matt/Ben?

There is no reason to allow emitters to exist once ParticleEngine::destroy() is called in shutdownGame().
#5
05/08/2006 (7:51 am)
Trying to put this into TSE MS3. Everything is mostly the same, except the final destroy() doesnt exist.

Still trying to find an appropriate place to insert the removeAllEmitters.

Besides putting it into the quit() console method, anyone have good suggestions for placement?
#6
05/29/2006 (12:09 pm)
Did you ever find an appropriate place to place it?
#7
05/29/2006 (12:29 pm)
Nope - looked around, never completed it, got distracted by something else.

So many tasks :-)
#8
05/29/2006 (1:25 pm)
I just saw this thread so I'm going to start putting this into TSE right now. Thomas, if you figure it out, please post back here =)

edit:
I think I got it, particleEngine.destroy() was called from shutdownGame in main.cc in TGE,
so I threw in an #include "game/fx/particleEmitter.h"

and added the ParticleEmitter::removeAllEmitters(); line just under PathManager::destroy(); in the same function in main.cpp