I found a couple ways:
You can use FObjectFinder
to get the particle system you need and then add it as a template or you can set your character with a default variable that will hold the particle system, which you can set in blueprints (this is how they do it in ShooterGame).
##FObjectFinder method:
###.h
#include "ParticleDefinitions.h"
TSubobjectPtr<UParticleSystemComponent> MyParticleSystem;
virtual void BeginPlay() OVERRIDE;
###.cpp (this code goes in the constructor)
ConstructorHelpers::FObjectFinder<UParticleSystem> ArbitraryParticleName(TEXT("ParticleSystem'/Game/Path/To/ParticleSystem/PS.PS'"));
MyParticleSystem = PCIP.CreateDefaultSubobject<UParticleSystemComponent>(this, TEXT("ArbitraryParticleName"));
if (ArbitraryParticleName.Succeeded()) {
MyParticleSystem->Template = ArbitraryParticleName.Object;
}
MyParticleSystem->bAutoActivate = false;
MyParticleSystem->SetHiddenInGame(false);
In order to get the path to your particle system you need to open the editor, right click the particle system in the content browser and choose copy reference
.
Now you need to attach your particle system to a scene component. I found it’s best to do this in BeginPlay()
since you might have components that you set up in blueprints rather than code. For example I do not set my root component in code, I just do it in blueprints.
void YourClass::BeginPlay() {
Super::BeginPlay();
MyParticleSystem->AttachTo(Cast<USceneComponent>(FindComponentByClass(UStaticMeshComponent::StaticClass())));
}
The above attaches to a static mesh component, but of course you want to attach to whatever component you need.
Now just call ActivateSystem or DeactivateSystem whenever.
MyParticleSystem->ActivateSystem();
##ShooterGame method:
This doesn’t involve as much code, but requires you to set the particle system in your blueprint.
###.h
#include "ParticleDefinitions.h"
#include "EngineKismetLibraryClasses.h"
/** effect played on respawn */
UPROPERTY(EditDefaultsOnly, Category=Pawn)
UParticleSystem* RespawnFX;
###.cpp
if (RespawnFX)
{
UGameplayStatics::SpawnEmitterAtLocation(this, RespawnFX, GetActorLocation(), GetActorRotation());
}
If you want your particle system to be attached you would use:
UGameplayStatics::SpawnEmitterAttached(RespawnFX, Cast<USceneComponent>(FindComponentByClass(UStaticMeshComponent::StaticClass())));
Again, attaching to a static mesh component. I haven’t really found a better way of getting components so I don’t know what your results will be if you have more than one type of component, ie. two static meshes, in your hierarchy.