Spawn(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(created)
 
m (→‎top: Unicodifying, replaced: [[Image: → [[File:)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Image:Entity init.png|right|The Source engine entity initialisation process.]]
{{lang|Spawn()}}
[[File:Entity init.png|right|130px|The Source engine entity initialisation process.]]


'''<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. By default it executes no commands (n.b. some base classes may change this).
'''<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.
 
{{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 ==


void CNPC_Antlion::Spawn()
<source lang="cpp">
{
void CNPC_Antlion::Spawn()
Precache();
{
Precache();
SetHullType( HULL_MEDIUM );
 
SetHullSizeNormal();
SetHullType( HULL_MEDIUM );
SetDefaultEyeOffset();
SetHullSizeNormal();
SetDefaultEyeOffset();
SetNavType( NAV_GROUND );
SetNavType( NAV_GROUND );
SetSolid( SOLID_BBOX );
 
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
 
SetMoveType( MOVETYPE_STEP );
SetMoveType( MOVETYPE_STEP );
 
...
...
}
}
</source>


== See also ==
== See also ==
Line 32: Line 38:
*[[Activate()]]
*[[Activate()]]
*[[Think()]]
*[[Think()]]
*[[My First Entity]]
*[[Your First Entity]]


[[Category:Functions]]
[[Category:Functions]]

Latest revision as of 00:17, 7 January 2024

English (en)Deutsch (de)Translate (Translate)
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