Spawn

From ZDoom Wiki
Jump to: navigation, search

int Spawn (str classname, fixed x, fixed y, fixed z [, int tid [, int angle]])

Usage

Spawns an actor at the given X, Y and Z coordinates. Optionally a TID and a byte angle can be specified.

The coordinates are specified in fixed point, which are not the same as grid units in your map editor. To convert a grid unit to a fixed point, you multiply the coordinate by 65536, or use fixed point literals.

The Z coordinate is also absolute, i.e. not relative to the floor height of the sector. So if you want something to spawn 128 units above a floor that is at a height of 64, then the Z coordinate needs to be 192.

Note that, unlike Thing_Spawn, Spawn does not create teleport fog. If you want teleport fog to appear, use the function again to spawn it manually.

To spawn something at the location of a MapSpot, use SpawnSpot instead.

To get a list of the things you can spawn in the game, visit the Classes page.

The return value is the number of things spawned.

Examples

This script spawns a Cacodemon above the player about three seconds after he enters the map. There is a 25% chance to instead spawn a Pain Elemental. Since the script is dependent upon the player's location there is the potential for failure due to coordinate obstruction. In an attempt to account for this, the script analyzes the return value of Spawn to determine success or failure, and upon failure will wait a tic and attempt the spawn again.

 script 1 ENTER
 {
   str class = "Cacodemon";
 
   if (!random(0, 3))
     class = "PainElemental";
 
   delay(104);
     
   do {
     delay(1);
     int x = GetActorX(0);
     int y = GetActorY(0);
     int z = GetActorZ(0) + 100.0;
     int angle = GetActorAngle(0) >> 8;
 
   } until (Spawn(class, x, y, z, 0, angle));
 }

In this example Health Bonuses will "rain" down on the activating player n number of times, where n is the amount passed to the script argument. The effect is achieved by spawning the bonuses above the player's head and at randomized x and y coordinates. This example spawns in a square pattern.

 script 1 (int n)
 {
   if (n < 0)
     n = -n;
 
   if (PlayerNumber() > -1 && n)
   {
     print(s:"It's raining meds!");
 
     while (n > 0)
     {
       int x = GetActorX(0) + random(-64.0, 64.0);
       int y = GetActorY(0) + random(-64.0, 64.0);
       int z = GetActorCeilingZ(0) - 8.0;
 
       if (Spawn("HealthBonus", x, y, z))
         n--;
 
       delay(1);
     }
   }
 }

This example uses some trigonometric functions to spawn the bonuses in a circular pattern.

 script 2 (int n)
 {
   if (n < 0)
     n = -n;
 
   if (PlayerNumber() > -1 && n)
   {
     print(s:"It's raining meds!");
 
     while (n > 0)
     {
       int angle = random(0, 1.0);
       int radius = random(0, 64.0);
       int x = GetActorX(0) + FixedMul(cos(angle), radius);
       int y = GetActorY(0) + FixedMul(sin(angle), radius);
       int z = GetActorCeilingZ(0) - 8.0;
 
       if (Spawn("HealthBonus", x, y, z))
         n--;
 
       delay(1);
     }  
   }
 }
ACS spawn functions
Spawn SpawnForced
SpawnSpot SpawnSpotForced
SpawnSpotFacing SpawnSpotFacingForced
SpawnProjectile