In the tutorial for Variables, timers and, events near the end, you are shown how to create a particle emitter via Blueprint. I was curious about how to achieve this through code as opposed to Blueprints. I have checked out the Particle systems documentation but am unsure of how to reference the starter content “P_Explosion” in my code. Any direction would be much appreciated.
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.
Thanks, that answered it.
Hey Epic Games, you know what would be great? If when someone like me goes to your documentation for the particle system component I see these code snips at the top of the page. You guys should take a look at the documentation for SFML, it’s so much better…
For some reason I get an EXCEPTION_ACCESS_VIOLATION on the line of the FObjectFinder. Any idea how this could happen?
Funny… I was hoping to do exactly what OP wanted to do… load the particle system with c++ only. I can confirm your advice worked for me in UE5.1.