I’m having trouble finding examples of how to spawn an explosion effect using pure c++ without the aid of a blueprint. I want to play the P_Explosion effect at an impact point. I have the coordinates, but I can’t figure out how to play the particle effect there.
FHitResult Hit;
FVector SpawnLocation = Hit.ImpactPoint;
FRotator SpawnRotation = Hit.ImpactNormal.Rotation();
UParticleSystem* EmitterTemplate = your ref to P_Explosion
UGameplayStatics::SpawnEmitterAtLocation(WorldContextObject, EmitterTemplate, SpawnLocation, SpawnRotation);
I tried that. However, there’s the issue of getting a reference to the template. It seems that you need a blueprint in order to get the template.
You should use static ConstructorHelpers::FObjectFinder to find the reference to the asset if you don’t want to expose the system and set it in blueprint.
and how do you get a callback to destroy the actor after the explosion is finished? you don’t want to leave this garbage there do you?
Yeah, you still need to tell it what template to use. You either hardcode it using FObjectFinder or StaticLoadObject (and thus have a C++ class for every different explosion you want to make) - or you use a Blueprinted version of your explosion class and set the defaults there.
@nullbot - You can tell Emitters to auto-destroy themselves when they finish.
make sure it loops once and destroy on finish are on
C++ class for every different explosion?
I did it this way;
in .h
USTRUCT(BlueprintType)
struct WHATEVS_API FParticleEffects {
GENERATED_USTRUCT_BODY()
public:
FParticleEffects {}
TArray<UParticleSystem*> aExplosions;
};
extern FParticleEffects ParticleEffects;
in .cpp
FParticleEffects ParticleEffects;
AMyClass::AMyClass(const FObjectInitializer& ObjectInitializer) {
ConstructorHelpers::FObjectFinder<UParticleSystem> PEffectQuery = TEXT("ParticleSystem'/Game/Art/ParticleEffects/SomeExplosion1'");
ParticleEffects.aExplosions.Add(PEffectQuery.Object);
PEffectQuery = (TEXT("ParticleSystem'/Game/Art/ParticleEffects/SomeExplosion2'"));
ParticleEffects.aExplosions.Add(PEffectQuery.Object);
PEffectQuery = (TEXT("ParticleSystem'/Game/Art/ParticleEffects/SomeExplosion3'"));
ParticleEffects.aExplosions.Add(PEffectQuery.Object);
.
.
.
PEffectQuery = (TEXT("ParticleSystem'/Game/Art/ParticleEffects/SomeExplosionN'"));
ParticleEffects.aExplosions.Add(PEffectQuery.Object);
}
This way your struct which holds all your particle effects is available to all classes which include your .h-file via the extern keyword.
UStructs can also hold functions if you’d like to for example get a random explosion emitter.
Interesting, different strokes I guess. I personally prefer to just abstract the class with a Blueprint and set the defaults.