How do I attach a particle system to a character class through code?

Hi! I would like to know if there’s a way to attach a particle system in a character class I’ve created, I tried with

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = render) TSubobjectPtr class UParticleSystemComponent ParticleComponent;

It didn’t work. I hope someone has the patience to enlight me. Thanks

#GameplayStatics

You want GameplayStatics!

See GameplayStatics.h for more info!

#.h
make sure to include:

//For the UGameplayStatics::SpawnEmitterAttached
#include "EngineKismetLibraryClasses.h"

//All particle definitions for using functions of UParticleSystemComponent
#include "ParticleDefinitions.h"

#.cpp

//effect
	UGameplayStatics::SpawnEmitterAttached(
		ThePS, //UParticleSystem*  
		CurrentPower, 
		TipSocketName, 
		FVector(-10,0,0), 
		FRotator(0,90,0), 
		EAttachLocation::KeepRelativeOffset, 
		true
	);

Thanks man, I’ll try it :slight_smile: (Y)

Replying to your forum post:

Hi there!

UGameplayStatics::SpawnEmitterAttached(
	ThePS,                   //particle system
	Mesh,      //mesh to attach to
	FName("Head"),   //socket name
	FVector(0,0,64),  //location relative to socket
	FRotator(0,0,0), //rotation 
	EAttachLocation::KeepRelativeOffset, 
	true //will be deleted automatically
);

Above example attaches a particle system to the “Head” socket of the Character, assuming this code is run inside your Character class .cpp

The particle system is spawned 64 units above the Character’s head socket.

"ThePS" must be pointer like this

UParticleSystem*

the easiest way to get the PS is to have a UPROPERTY() on the BP for your class and fill it in the Editor in the defaults for your blueprinted class in which you are doing this spawning (like a Character BP)

/** JoyPower Swirls */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=ParticleSystem)
UParticleSystem* JoyPowerSwirls;

:slight_smile:

Rama

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.

Thanks, I’ll try them to see which one is the best, many thanks!!! (Y)

Wow, thanks, I’ll study them to see which one is the best way to attach the particle system, thank you so much!!! (Y)

MyParticleSystem->AttachTo… is working
but is it possible to say MyParticleSystem->Attachparent = Rootcomponent ?
The Attachparent isn’t working for me but i cant figure out why…

I believe AttachParent is just what your particle system is currently attached to, but doesn’t actually setup the attachment. You’ll probably still have to use AttachTo to attach anything.