Download

Auto destroy a spawned ParticleSystem Component?

Hello, I have a spawned particle system that I’m trying to auto destroy.

It is snow falling, and after a certain period I want to be able for it to stop (when player moves inside a cave) and be replaced with another effect.
The problem is if I use SetActive(false), it’s ‘Kill on completed’ won’t trigger.

I want it to be able to destroy itself, so in the code I can immediately start up another ambient effect.


//.h
UPROPERTY() UParticleSystemComponent* AirParticle;

//.cpp

//Spawning
AirParticle = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), snow, FTransform::Identity);

//Set auto-destroy (doesn't work)
AirParticle->SetActive(false); // Kill on completed doesn't work
AirParticle->GetOwner()->SetLifeSpan(3); // Doesn't work because owner is 'WorldSettings' and not an actual Actor

//Spawn new particle in place, while old one auto destroys itself
AirParticle = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), cave, FTransform::Identity);

Any idea what to do here?

Edit 1:
I tried creating an empty actor so I could attach it and then set a lifetime on that actor, but I can’t get an empty actor to be created!

Edit 2:
If I just change the variable to a new component, will the old unreferenced one be automatically garbage collected?

You’re setting your lifespan on the World Settings object (which, yea, you definitely don’t want to do).

I’m not sure you can set the Lifetime of an entire Particle system given that each emitter within a system has its own unique lifetime parameter. You could, however, simply call DeactivateSystem when you want to turn the effect off.

Yeah I’m doing that (Same thing as SetActive(false)), however I also want the system to **destroy **itself when it’s finished deactivating, so it won’t sit around forever hogging up memory.

Ex: Player is in a snow area and then walks into a rain area:

ParticleSystemVar = Snow
//Walk into rain area
ParticleSystemVar ->Deactivate (And kill on completed)
ParticleSystemVar = Rain

^ Ideally it will switch to rain, and the old one will destroy itself when it’s finished deactivating, but that’s not the case. The old one to my knowledge just sits there forever, deactivated.
If the player ran between snow and rain often it could accumulate a ton of deactive systems, which is what I’m looking to avoid with it destroying itself when completed.

Would a deactivated, unreferenced particle system automatically become garbage collected?

As long as there are no references, yes. The GC system will automatically free that memory for you.