Beam particle system randomly disappears


I have a beam particle system, and a beam particle system component that I create from it, like so:

    static ConstructorHelpers::FObjectFinder<UParticleSystem> beam_finder(TEXT("ParticleSystem'/Game/Particles/beam.beam'"));
    m_beam_ps = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("beam_ps"));
    m_beam_ps->SetRelativeLocation(FVector(24.889f, 0.0f, 11.943f));

I’m using it as a weapon effect, so, in the editor, the source of the beam is set to “Emitter” and the target is set to “User Set.” When the player presses the fire button, the following happens:

  1. I set the beam to visible with m_beam_ps->SetVisibility(true).
  2. I do a line trace to see if it hit anything.
    3a) If it does hit something, I set the beam’s target point to whatever it hit.
    3b) If it didn’t hit anything, I set the target to a point off screen so the beam appears to just shoot out into space.

When the player releases the fire button, I just hide the beam again with m_beam_ps->SetVisibility(false).

This all works just fine, for a while, but the beam eventually just disappears. Calling SetVisibility(true) just won’t bring it back. I checked these:

    if (!m_beam_ps->IsVisible()) PRINTF(TEXT("not visible"));
    if (!m_beam_ps->IsActive()) PRINTF(TEXT("not active"));

And they’re both returning true while firing (neither message is ever printed).

Any idea why the particle system is disappearing?

Garbage Collection Protection

When you create your Beam PSC, make sure to store it in a global UPROPERTY() variable!

//.h file

UParticleSystemComponent* m_beam_ps;

Otherwise your particle is getting garbage collected !

I have a wiki on UE4 Garbage Colleciton Here:

Memory Management and GC in UE4



That’s really helpful. Thanks Rama.

I have to say that it’s a little aggressive for the engine to garbage collect something that’s still in use, though. I thought that garbage collection was for memory that has gone out of scope. I guess UE cheats a little to improve performance.