Spawn Review

The spawn function is used to instantiate and place an actor in the world. In the UDK, this function is a member function of the class Actor. Therefore, any actor object (that is, an object whose class that is a descendant of Actor) is able to spawn other actors.

Let's look at the signature of this function:

/** Spawn an actor. Returns an actor of the specified class, not
 * of class Actor (this is hardcoded in the compiler). Returns None
 * if the actor could not be spawned (if that happens, there will be a log warning indicating why)
 * Defaults to spawning at the spawner's location.
 *
 * @note: ActorTemplate is sent for replicated actors and therefore its properties will also be applied
 * at initial creation on the client. However, because of this, ActorTemplate must be a static resource
 * (an actor archetype, default object, or a bStatic/bNoDelete actor in a level package)
 * or the spawned Actor cannot be replicated
 */
native noexport final function coerce actor Spawn
(
    class<actor>      SpawnClass,
    optional actor      SpawnOwner,
    optional name     SpawnTag,
    optional vector   SpawnLocation,
    optional rotator  SpawnRotation,
    optional Actor    ActorTemplate,
    optional bool      bNoCollisionFail
);

Let's look at the parameters:

  • class<Actor> SpawnClass
    • The class of actor to spawn. Notice that the parameter is a class restrictor limiting this to classes descending from Actor. The spawn function can only instantiate classes that derive from Actor.

From this point forward, all following parameters are optional:

  • optional actor SpawnOwner
    • The owning actor. This defaults to None. If provided, the spawned actor's Owner variable is set to the actor that is passed in.
  • optional name SpawnTag
    • A name to set as the actor's tag variable. Defaults to '' (empty/zero-length name).
  • optional vector SpawnLocation
    • The location to place the actor at in the world. Defaults to the location of the actor on which the spawn function was called.
  • optional vector SpawnRotation
    • The rotation to rotate the actor to in the world. Defaults to the rotation of the actor on which the spawn function was called.
  • optional actor ActorTemplate
    • An actor template, archetype, or a static actor that could be used to initialize the default properties of the spawned actor. Typically set to None/skip.
  • optional bool bNoCollisionFail
    • Typically, a blocking actor cannot be spawned while colliding with something that is blocking it. If we pass true here, it means that a collision check is not performed and the actor is forcibly spawned.

Example

Consider the following use of the spawn function in the PostBeginPlay event of an example actor:

class ExampleActor extends Actor;

var class<Actor> ClassToSpawn;

function PostBeginPlay()
{
    local vector SpawnLoc;
    local rotator SpawnRot;
    local UTPawn spawnedPawn;

    SpawnLoc.Z = 500;       //500 units up
    SpawnRot.Pitch = 8192;  //45 degrees pitched up

    // We will spawn a UTPawn actor at the location 0, 0, 500 and rotated to pitch 45 degrees up.

    // The first way we will spawn using a class literal:
    spawnedPawn = spawn(class'UTPawn',,, SpawnLoc, SpawnRot);

    // The second way, we will use the class restrictor variable on this class:
    spawnedPawn = spawn(ClassToSpawn,,, SpawnLoc, SpawnRot);

    // Notice that in both situations we passed in SpawnLoc as the spawn location
    // and SpawnRot as the spawn rotation.
}

DefaultProperties
{
    ClassToSpawn=class'UTPawn'
}