I want to spawn explosions when an actor is destroyed. Seems easy, I just give each actor a UParticleSystem* pointer and initialize it in the actor’s constructor with whatever explosion I want for that particular actor. When the actor’s hitpoints reach zero, I call SpawnEmitterAtLocation() and pass in the UParticleSystem* pointer. The emitter is set to auto-destroy, one loop, etc. Mostly, it appears to work correctly.
Problem is that the particle system sometimes gets garbage collected (I think). I’ll have a screen full of explosions and they’ll all just periodically disappear. My guess is that the garbage collector runs, sees that the actor that owned the pointer has been destroyed, and cleans it because there are no remaining pointers to it.
I tried a really hacky solution of forcing the actor to hang around for a few seconds before destroying it, but that has worse problems. For one, each type of explosion might have a different duration, and I don’t know a way in C++ to get an emitter’s duration. For another, doing it this way means I have to play games with making the actor invisible and turning off its collision, etc. Not clean.