How do I reference my particle system in code?

Hi.

Direct references to assets in C++ code should be avoided. It’s better to declare particle system component in your header file like this:

UPROPERTY(VisibleAnywhere)
class UParticleSystemComponent* PSC;

Then create it in your constructor:

PSC = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyPSC"));

And then set it’s template in editor.

If you still need a reference to particle system, you can get it like this:

static ConstructorHelpers::FObjectFinder<UParticleSystem> PS(TEXT("ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
PSC = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyPSC"));
PSC->SetTemplate(PS.Object);

Hope this helps.

2 Likes