Particle system being garbage collected
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.
asked Feb 19 '17 at 11:55 PM in C++ Programming
Why not use the GameplayStatics instead of keeping a pointer to the Particle System Component on each actor? You can rather store a UParticleSystem* (i.e. not Component) reference on your actors and pass that to UGameplayStatics::SpawnEmitterAtLocation(...).
I might be wrong, but I think this uses the emitter pool so you will possibly gain a large performance benefit as well.
Follow this question
Once you sign in you will be able to subscribe for any updates here