How to manage lots of Particle Emitter (Managers)?

I create lots of Particle Emitters resp. lots of Particle Emitter Managers. Now I’m wondering, what is the best practice to deal with them efficiently?

I’m pretty sure that leaving them hang around is a bad idea.

Should I have only one Particle Emitter Manager and create Emitters wherever needed?

Should I add and destroy the Particle Emitter Managers as I go?

Or should I create some sort of pool to manage them?

You could pool them, but you might not have to.

For most situations you need only one manager per particle texture. You can create as many emitters as you need from one manager. Remove an emitter you never want to use again. To disable an emitter temporarily, you can set its on = false and visible = false.

Ok, I’ll try to be a little more specific. In my game there will be lots of explosions all over the place. I actually call emitter.explode() for them. Thus I think calling on = false or visible = false isn’t necessary, because after the explosion nothing is visible anyway.

Eventually, an explosion will (probably) happen at the same coordinates again. My question is: should I keep the same emitter around for this particular explosion? Lots of other explosions will happen in other places until another explosion will happen at the same coordinates. If I keep an emitter around for every location an explosion might occur, their numbers will probably go into the hundreds.

I can think of two alternatives:

  1. I could maintain a pool of emitters I use whenever an explosion is due somewhere. Downside: I need to implement some logic for the pool.

  2. I just create and remove emitters all the time. Downside: Not sure. Is creating and removing emitters rather expensive? Or is that not much more overhead than setting on = false?

All these explosions look the same: same texture, same particle emitter config.

I would create a list of emitters, then pool them. I would probably do this by calling shift() on the list to remove and get the first emitter and setting the position to wherever an explosion occurs. Then I would call explode() on the returned emitter. Then setTimeout() for explosion length or use one of the callbacks on the emitter, and push the shifted element to the end of the list once the animation is finished. For this implementation, you need to make sure there is an emitter for each explosion visible. For example, if there were 10 explosions, you should have that many emitters. Or basically, just the max amount of explosions that could occur in the time it takes an emitter to finish. And for safety, I would also check if the list.length > 0 to ensure it doesn’t try to grab an emitter when there are none at that moment.

If you use only explode() then you need only one emitter. Just reposition it for each explosion.

Awesome. I didn’t think I could move the emitter while there are still particles about. But I can.

@nboj thanks for your explanation too. I will follow your advice once I create more complicated effects.

1 Like