x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: Not Selected
Tags:
more ▼

asked Apr 23 '14 at 05:04 AM in C++ Programming

avatar image

Aragonath
130 8 9 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

4 answers: sort voted first

GameplayStatics

You want GameplayStatics!

See GameplayStatics.h for more info!

.h

ake 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
     );

more ▼

answered Apr 23 '14 at 06:31 AM

avatar image

Rama
10.7k 446 346 1090

avatar image Aragonath Apr 24 '14 at 12:14 AM

Thanks man, I'll try it :) (Y)

avatar image Rama Apr 24 '14 at 07:18 AM

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;

:)

Rama

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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:

his 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.

more ▼

answered May 28 '14 at 12:11 AM

avatar image

Kochab
563 59 164 125

avatar image oOo.DanBO.oOo Nov 09 '14 at 05:07 PM

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...

avatar image Kochab Nov 10 '14 at 12:10 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

more ▼

answered May 28 '14 at 11:55 PM

avatar image

Aragonath
130 8 9 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

more ▼

answered May 28 '14 at 11:57 PM

avatar image

Aragonath
130 8 9 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question