# How to attach a particle system to a spawned actor through code?

**URL:** <https://forums.unrealengine.com/t/how-to-attach-a-particle-system-to-a-spawned-actor-through-code/446475>\
**Category:** FX\
**Tags:** particle-emitter, attach-to-actor, attachment, unreal-engine, CPP, question\
**Created:** [April 2, 2019, 8:37pm UTC](https://forums.unrealengine.com/t/how-to-attach-a-particle-system-to-a-spawned-actor-through-code/446475 "2019-04-02T20:37:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JTSmiff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JTSmiff](https://forums.unrealengine.com/u/JTSmiff)\
**Post date:** [April 2, 2019, 8:37pm UTC](https://forums.unrealengine.com/t/how-to-attach-a-particle-system-to-a-spawned-actor-through-code/446475/1 "2019-04-02T20:37:13Z")

</div>

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);
	}			
	
}
```

---

<div class="post-metadata">

**Author:** ![atanknomore](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@atanknomore](https://forums.unrealengine.com/u/atanknomore)\
**Post date:** [April 2, 2019, 9:42pm UTC](https://forums.unrealengine.com/t/how-to-attach-a-particle-system-to-a-spawned-actor-through-code/446475/2 "2019-04-02T21:42:15Z")

</div>

First of all, the function goes as follows:

> **[UGameplayStatics::SpawnEmitterAttached](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Kismet/UGameplayStatics/SpawnEmitterAttached/2/)**
>
> Plays the specified effect attached to and following the specified component.

```
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 🙂

---

<div class="post-metadata">

**Author:** ![JTSmiff](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JTSmiff](https://forums.unrealengine.com/u/JTSmiff)\
**Post date:** [April 2, 2019, 10:20pm UTC](https://forums.unrealengine.com/t/how-to-attach-a-particle-system-to-a-spawned-actor-through-code/446475/3 "2019-04-02T22:20:04Z")

</div>

This worked perfectly thanks 🙂
