Activate(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Made the note much clearer and more descriptive)
m (Nesciuse moved page Activate()/en to Activate() without leaving a redirect: Move en subpage to basepage)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Image:Entity init.png|right|The Source engine entity initialisation process.]]
{{LanguageBar}}
 
[[File:Entity init.png|right|The Source engine entity initialisation process.]]


'''<code>Activate()</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 after <code>[[Spawn()]]</code> and, if <code>Spawn()</code> occurred during a map's initial load, after all other entities have been spawned too.
'''<code>Activate()</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 after <code>[[Spawn()]]</code> and, if <code>Spawn()</code> occurred during a map's initial load, after all other entities have been spawned too.
Line 9: Line 11:
{{tip|<code>Activate()</code> is not related to entity [[input]]s or [[flag]]s of the same name. It is ''always'' called.}}
{{tip|<code>Activate()</code> is not related to entity [[input]]s or [[flag]]s of the same name. It is ''always'' called.}}


{{note|<code>Activate()</code> is only called for entities placed on the map in hammer.  If an entity is spawned from the console (ex:<code>ent_create</code>) or via game code (ex:<code>CreateEntityByName()</code>) then <code>Activate()</code>  is ''not'' called.<br>
{{note|If an entity is spawned from the console (ex:<code>ent_create</code>) or via game code (ex:<code>CreateEntityByName()</code>) then <code>Activate()</code>  is ''not'' called so be sure to call <code>Activate()</code> from the spawning code.  Also note that <code>Activate()</code> is not called for client-side entities.<br>.}}
For server-side entities spawned from game code <code>Activate()</code> can be called by the spawning code.  Otherwise critical code should be in <code>Spawn()</code> or the entity's think function.<br>
Another idea for networked entities is to place a block of code in <code>OnDataChanged()</code> when <code>updateType</code> is <code>DATA_UPDATE_CREATED</code>.  This indicates when the client-side entity has sucessfully connected to the server-side entity.  This occurs on network entities after <code>Activate()</code> but before any thinking. '''Note:''' when using this idea be sure to call <code>BaseClas::OnDataChanged()</code> '''First'''.}}


== Example ==
== Example ==
Line 28: Line 28:
*[[Think()]]
*[[Think()]]
*[[Your First Entity]]
*[[Your First Entity]]
[[Category:Functions]]
[[Category:Functions]]

Latest revision as of 03:57, 12 July 2024

English (en)Русский (ru)Translate (Translate)
The Source engine entity initialisation process.

Activate() 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 after Spawn() and, if Spawn() occurred during a map's initial load, after all other entities have been spawned too.

Activate() is used to perform spawning tasks that require interaction with other entities. Such tasks should never be performed in Spawn() itself, as it may be called before any other entities become available.

Warning.pngWarning:CBaseEntity executes code in this function, so always call BaseClass::Activate() from your version.
Tip.pngTip:Activate() is not related to entity inputs or flags of the same name. It is always called.
Note.pngNote:If an entity is spawned from the console (ex:ent_create) or via game code (ex:CreateEntityByName()) then Activate() is not called so be sure to call Activate() from the spawning code. Also note that Activate() is not called for client-side entities.
.

Example

void CAlyxEmpEffect::Activate()
{
	// Start out with a target entity
	SetTargetEntity( STRING(m_strTargetName) );
	
	BaseClass::Activate();
}

See also