Spawn(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(note on player spawn())
No edit summary
Line 3: Line 3:
'''<code>Spawn()</code>''' is a <code>void</code> member function of <code>[[CBaseEntity]]</code> 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 <code>BaseClass::Spawn()</code> from your version.
'''<code>Spawn()</code>''' is a <code>void</code> member function of <code>[[CBaseEntity]]</code> 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 <code>BaseClass::Spawn()</code> from your version.


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.
<code>Spawn()</code> 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.


{{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.}}
Variable initialisation should be performed in the [[Wikipedia:Constructor (object-oriented programming)|class constructor]] rather than <code>Spawn()</code> to avoid overwriting data from the map's entity block.


{{note|<code>[[CBasePlayer]]::Spawn()</code> is called when the player respawns, too. Use <code>InitialSpawn()</code> for code that should only run once.}}
{{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 entity 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.}}
 
{{tip|<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 13:09, 31 May 2011

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