Spawn(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(note on player spawn())
Line 5: Line 5:
Spawn() is used to perform tasks that must take place before the entity can enter the world, typically including calling <code>[[Precache()]]</code> and <code>[[SetSolid()]]</code>, initialising [[VPhysics]] simulation and setting up global AI parameters. It is recommended that variable initialisation occur in the class constructor rather than Spawn() to avoid code clutter in either location, but this is up to the programmer.
Spawn() is used to perform tasks that must take place before the entity can enter the world, typically including calling <code>[[Precache()]]</code> and <code>[[SetSolid()]]</code>, initialising [[VPhysics]] simulation and setting up global AI parameters. It is recommended that variable initialisation occur in the class constructor rather than Spawn() to avoid code clutter in either location, but this is up to the programmer.


{{warning|<code>Spawn()</code> 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 <code>Spawn()</code> function itself. Therefore, any code which requires the entity to search or otherwise link itself to other entities is usually unreliable. Use <code>[[Activate()]]</code> for these tasks, which is called after all spawning has completed.}}
{{warning|<code>Spawn()</code> 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 <code>Spawn()</code> function itself). Therefore, any code which requires the entity to search or otherwise link itself to other entities is usually unreliable. Use <code>[[Activate()]]</code> for these tasks, which is called after all spawning has completed.}}
 
{{note|<code>[[CBasePlayer]]::Spawn()</code> is called when the player respawns, too. Use <code>InitialSpawn()</code> for code that should only run once.}}


== Example ==
== Example ==

Revision as of 16:27, 1 February 2010

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. It is recommended that variable initialisation occur in the class constructor rather than Spawn() to avoid code clutter in either location, but this is up to the programmer.

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 Spawn() function 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.
Note.pngNote: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