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 to attach a particle system to a spawned actor through code?

I'm trying to create a blueprint that will let me spawn an actor and a particle emitter at a location with the option of having the emitter attached to the actor. So far I can't nail down how exactly to use SpawnEmitterAttached in this context with an actor being spawned by the same function. Also to note, the actor I'm trying to spawn has no other component beyond its static mesh.

.h

 #include "CoreMinimal.h"
 #include "Kismet/BlueprintFunctionLibrary.h"
 #include "TechTestLibrary.generated.h"
 
 
 UCLASS()
 class TECHTEST2_API UTechTestLibrary : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()
     
         UFUNCTION(BlueprintCallable, meta = (WorldContext = "RefObj"))
         static void TestSpawn(
             TSubclassOf<AActor> Actor,
             UParticleSystem* Emitter,
             FTransform Transform,    
             int Amount,
             bool Attached,
             ESpawnActorCollisionHandlingMethod SpawnCollisionHandlingOverride,
             UObject* RefObj            
         );
 };

.cpp

 void UTechTestLibrary::TestSpawn(
     TSubclassOf<AActor> Actor,
     UParticleSystem* Emitter,
     FTransform Transform,
     int Amount,
     bool Attached,
     ESpawnActorCollisionHandlingMethod SpawnCollisionHandlingOverride,
     UObject* RefObj    
 )
 {
     UWorld* TechTest = GEngine->GetWorldFromContextObject(RefObj);
                     
     for (int i = 0; i < Amount; i++)
     {
         AActor* TestActor = TechTest->SpawnActor<AActor>(Actor, Transform);
 
         if (Attached) 
         {
             UGameplayStatics::SpawnEmitterAttached(Emitter, TestActor, NAME_None,
                 Transform, EAttachLocation::SnapToTarget, false);
 
         }        
     }
 
     if (!Attached) 
     {
         UGameplayStatics::SpawnEmitterAtLocation(TechTest, Emitter, Transform, true);
     }            
     
 }









Product Version: UE 4.21
Tags:
more ▼

asked Apr 02 '19 at 08:37 PM in C++ Programming

avatar image

JTSmiff
3 1 1

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

1 answer: sort voted first

First of all, the function goes as follows:

https://api.unrealengine.com/INT/API/Runtime/Engine/Kismet/UGameplayStatics/SpawnEmitterAttached/2/index.html

 static UParticleSystemComponent * SpawnEmitterAttached
 (
     class UParticleSystem * EmitterTemplate,
     class USceneComponent * AttachToComponent,
     FName AttachPointName,
     FVector Location,
     FRotator Rotation,
     FVector Scale,
     EAttachLocation::Type LocationType,
     bool bAutoDestroy,
     EPSCPoolMethod PoolingMethod
 )

If you're using this function, use it as follows:

     if (TestActor && YourTemplate && (YourScale.Size() != 0))
     {
      UGameplayStatics::SpawnEmitterAttached(YourTemplate, TestActor->GetRootComponent(), NAME_None, YourLocation, YourRotation, YourScale, EAttachLocation::SnapToTarget, false, EPSCPoolMethod::AutoRelease
     }

That should work :)

more ▼

answered Apr 02 '19 at 09:42 PM

avatar image

me4dinosaur
251 15 14 20

avatar image JTSmiff Apr 02 '19 at 10:20 PM

This worked perfectly thanks :)

(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