Spawn()

From Valve Developer Community
Jump to: navigation, search
English (en)Deutsch (de)
... Icon-Important.png
The Source engine entity initialisation process.

Spawn() is a void member function of CBaseEntity that is available through inheritance to every entity in a Source game. It is called by the engine whenever a new instance of an entity is created, much like a constructor. Some base classes execute code from it, so to be safe always call BaseClass::Spawn() from your version.

Spawn() is used to perform tasks that must take place before the entity can enter the world, typically including calling Precache() and SetSolid(), initialising VPhysics simulation and setting up global AI parameters.

Variable initialisation should be performed in the class constructor rather than Spawn() to avoid overwriting data from the map's entity block.

Warning.pngWarning:Spawn() is called immediately after the creation of the entity. If this has occurred during level load there is no guarantee that other entities have been spawned yet (unless they are spawned by the entity itself). Therefore, any code which requires the entity to search or otherwise link itself to other entities is usually unreliable. Use Activate() for these tasks, which is called after all spawning has completed.
Tip.pngTip:CBasePlayer::Spawn() is called when the player respawns, too. Use InitialSpawn() for code that should only run once.

Example

void CNPC_Antlion::Spawn()
{
	Precache();

	SetHullType( HULL_MEDIUM );
	SetHullSizeNormal();
	SetDefaultEyeOffset();
	
	SetNavType( NAV_GROUND );

	SetSolid( SOLID_BBOX );
	AddSolidFlags( FSOLID_NOT_STANDABLE );

	SetMoveType( MOVETYPE_STEP );

	...
}

See also