Activate()

From Valve Developer Community
Revision as of 17:52, 29 December 2009 by TerabyteDragon (talk | contribs) (Made the note much clearer and more descriptive)
Jump to navigation Jump to search
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:Activate() is only called for entities placed on the map in hammer. If an entity is spawned from the console (ex:ent_create) or via game code (ex:CreateEntityByName()) then Activate() is not called.

For server-side entities spawned from game code Activate() can be called by the spawning code. Otherwise critical code should be in Spawn() or the entity's think function.

Another idea for networked entities is to place a block of code in OnDataChanged() when updateType is DATA_UPDATE_CREATED. This indicates when the client-side entity has sucessfully connected to the server-side entity. This occurs on network entities after Activate() but before any thinking. Note: when using this idea be sure to call BaseClas::OnDataChanged() First.

Example

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

See also