SpawnActor Method
The process of creating a new instance of an Actor is known as spawning. Spawning of Actors is performed using the UWorld::SpawnActor()
function. This function creates a new instance
of a specified class and returns a pointer to the newly created Actor. UWorld::SpawnActor()
may only be used for creating instances of classes which inherit from the Actor class in their
hierarchy.
AActor* UWorld::SpawnActor
(
UClass* Class,
FName InName,
FVector const* Location,
FRotator const* Rotation,
AActor* Template,
bool bNoCollisionFail,
bool bRemoteOwned,
AActor* Owner,
APawn* Instigator,
bool bNoFail,
ULevel* OverrideLevel,
bool bDeferConstruction
)
Parameter |
Description |
---|---|
|
A |
|
(Optional) An |
|
(Optional) An |
|
(Optional) An |
|
(Optional) An |
|
(Optional) A |
|
(Optional) |
|
(Optional) The |
|
(Optional) The |
|
(Optional) A |
|
(Optional) The |
|
(Optional) A |
Return Value |
|
The spawned Actor in the form of an |
Usage
AKAsset* SpawnedActor1 = (AKAsset*) GetWorld()->SpawnActor(AKAsset::StaticClass(), NAME_None, &Location);
Spawn Function Templates
In order to make spawning Actors more friendly, several function templates are provided for the most common usage patterns. These make creating Actors much simpler as they require a smaller subset of parameters and allow the type of the returned Actor to be specified.
Spawn T Instance, Return T Pointer
This function template spawns an instance of the template class T
at the same location, and with the same rotation, as the root component of the Actor performing the spawn operation, and returns
a pointer to that instance of the same type as the template class, i.e. T*
. The owning Actor, instigating Pawn, and whether the spawn operation should fail if the spawned Actor
encroaches, or would be colliding with another Actor already present in the world can be specified.
/** Spawns and returns class T, respects default rotation and translation of root component. */
template< class T >
T* SpawnActor
(
AActor* Owner=NULL,
APawn* Instigator=NULL,
bool bNoCollisionFail=false
)
{
return (T*)(GetWorld()->SpawnActor(T::StaticClass(), NAME_None, NULL, NULL, NULL, bNoCollisionFail, false, Owner, Instigator));
}
Usage
MyHUD = SpawnActor<AHUD>(this, Instigator);
Spawn T Instance with Transform, Return T Pointer
This function template spawns an instance of the template class T
at the specified Location
and with the specified Rotation
, and returns a pointer to that instance of the same type as the
template class, i.e. T*
. In addition to the location and rotation, the owning Actor, instigating Pawn, and whether the spawn operation should fail if the spawned Actor encroaches, or
would be colliding with, another Actor already present in the world can be specified.
/** Spawns and returns class T, forcibly sets world position. */
template< class T >
T* SpawnActor
(
FVector const& Location,
FRotator const& Rotation,
AActor* Owner=NULL,
APawn* Instigator=NULL,
bool bNoCollisionFail=false
)
{
return (T*)(GetWorld()->SpawnActor(T::StaticClass(), NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator));
}
Usage
Controller = SpawnActor<AController>(GetLocation(), GetRotation(), NULL, Instigator, true);
Spawn Class Instance, Return T Pointer
This function template spawns an instance of the specified Class
at the same location, and with the same rotation, as the root component of the Actor performing the spawn operation, and returns
a pointer to that instance cast to the type of the template class, i.e. T*
. This requires that the specified Class
be a child of the template class T
. In addition to the class, the owning
Actor, instigating Pawn, and whether the spawn operation should fail if the spawned Actor encroaches, or would be colliding with, another Actor already present in the world can be
specified.
/** Spawns given class and returns class T pointer, respects default rotation and translation of root component. */
template< class T >
T* SpawnActor
(
UClass* Class,
AActor* Owner=NULL,
APawn* Instigator=NULL,
bool bNoCollisionFail=false
)
{
return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, NULL, NULL, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
}
Usage
MyHUD = SpawnActor<AHUD>(NewHUDClass, this, Instigator);
Spawn Class Instance with Transform, Return T Pointer
This function template spawns an instance of the specified Class
at the specified Location
and with the specified Rotation
, and returns a pointer to that instance of the same type as the
template class, i.e. T*
. This requires that the specified Class
be a child of the template class T
. In addition to the class, location, and rotation, the owning Actor, instigating
Pawn, and whether the spawn operation should fail if the spawned Actor encroaches, or would be colliding with, another Actor already present in the world can be specified.
/** Spawns given class and returns class T pointer, forcibly sets world position. */
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;
}
Usage
APawn* ResultPawn = SpawnActor<APawn>(DefaultPawnClass, StartLocation, StartRotation, NULL, Instigator);