From Valve Developer Community
Revision as of 12:50, 2 March 2006 by ^Ben (talk | contribs) (t)

Jump to: navigation, search

A common way to address a specific entity is by its entity index ( CBaseEntity::entindex() ). Each time a new entity is instantiated, the engine looks up an unused entity index and assigns it to the new entity. Once the entity object is destroyed, its entity index becomes available again and may be reused for a different entity. Therefore the entity index is not a good way to reference a specific entity over longer times. A better and safer way is to use EHANDLEs (or CBaseHandle) to keep track of entity instances. EHANDLEs encapsulate a 32-bit ID that is unique for an entity instance during the whole game and can be used on both server and client to refer to entity objects (EHANDLEs are a combination of entity index and an increasing serial number). An EHANDLE can be transformed to CBaseEntity and vice versa just by using its overloaded operators. If an EHANDLE resolves to NULL, the entity object is not valid anymore.

// find a player, if no player is available pPlayer and hPlayer are NULL
EHANDLE hPlayer = gEntList.FindEntityByClassname( NULL, "player" );
CBaseEntity *pPlayer = hPlayer; // convert EHANDLE into entity pointer
m_hPlayer = pPlayer; // convert entity pointer to EHANDLE

Related content