# Trouble spawning an actor

**URL:** <https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [August 1, 2016, 7:13pm UTC](https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820 "2016-08-01T19:13:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_abd980be](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_abd980be](https://forums.unrealengine.com/u/anonymous_user_abd980be)\
**Post date:** [August 1, 2016, 7:13pm UTC](https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820/1 "2016-08-01T19:13:47Z")

</div>

This is more of a “I don’t understands templates or function templates” post, and I’m hoping someone can help me out with understand how they work.

I am trying to spawn my actor, and I have successfully done so by doing

```
APawn* ResultPawn = (APawn*)GetWorld()->SpawnActor(APawn::StaticClass());

```

The issue is I want to spawn it at a certain location with a certain rotation, and this [Spawning Actors in Unreal Engine | Unreal Engine 5.1 Documentation](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/Spawning/) says I need to use templates for that.

I gave it a shot by literally copying the last example: (.h)

```
template< class T >
T* SpawnActor
(
    UClass* Class,
    FVector const& Location,
    FRotator const& Rotation,
    AActor* Owner=NULL,
    APawn* Instigator=NULL,
    bool bNoCollisionFail=false
)
{
    return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
}

```

(.cpp):

```
APawn* ResultPawn = SpawnActor(DefaultPawnClass, StartLocation, StartRotation, NULL, Instigator);

```

so this is my code:

(.h)

```
	template< class T >
	T* SpawnActor
	(
		UClass* Class,
		FVector const& Location,
		FRotator const& Rotation,
		AActor* Owner = NULL,
		APawn* Instigator = NULL,
		bool bNoCollisionFail = false
	)
	{
		return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
	}

```

(.cpp)

```
	const FVector startLocation = FVector(-8908.868164f, -12233.826172f, 3496.578613f);
	const FRotator startRotation = FRotator(0.f, 0.f, 0.f);
	
	ATower_Archer* ResultPawn = SpawnActor<ATower_Archer>(ATower_Archer::StaticClass(), startLocation, startRotation, NULL, Instigator);

```

I get these errors:

```
[2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error e:\users\*\documents\unreal projects\towerdefenseproject\source\towerdefenseproject\MainPawn.h(124) : error C2661: 'UWorld::SpawnActor': no overloaded function takes 9 arguments
[2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error E:\Users\*\Documents\Unreal Projects\TowerDefenseProject\Source\TowerDefenseProject\MainPawn.cpp(48) : note: see reference to function template instantiation 'T *AMainPawn::SpawnActor<ATower_Archer>(UClass *,const FVector &,const FRotator &,AActor *,APawn *,bool)' being compiled

```

I followed the example exactly like it says and I can’t quite figure it out.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d2fc0364](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d2fc0364](https://forums.unrealengine.com/u/anonymous_user_d2fc0364)\
**Post date:** [August 1, 2016, 7:35pm UTC](https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820/2 "2016-08-01T19:35:23Z")

</div>

You don’t need to write the code for the template function - Unreal Engine has already provided that. You just need to call it. To call a template function, the class you want to use needs to go in the angle brackets. Like so:

```
ATower_Archer* ResultPawn = GetWorld()->SpawnActor<ATower_Archer>(startLocation, startRotation);

```

This should spawn an ATower\_Archer at the desired location and rotation. If you wanted to spawn another class, you’d just replace ATower\_Archer with AAnother\_Class.

```
AAnother_Class* ResultPawn = GetWorld()->SpawnActor<AAnother_Class>(startLocation, startRotation);

```

You only need that line of code - delete the declaration and definition of SpawnActor in your own header and cpp files.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_abd980be](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_abd980be](https://forums.unrealengine.com/u/anonymous_user_abd980be)\
**Post date:** [August 1, 2016, 7:40pm UTC](https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820/3 "2016-08-01T19:40:52Z")

</div>

Oh. Thanks. Just tried that and it worked.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d2fc0364](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d2fc0364](https://forums.unrealengine.com/u/anonymous_user_d2fc0364)\
**Post date:** [August 1, 2016, 7:49pm UTC](https://forums.unrealengine.com/t/trouble-spawning-an-actor/365820/4 "2016-08-01T19:49:31Z")

</div>

Great! Could you mark the answer as accepted? There’s a checkmark under the upvote/downvote button.
