CHandle: Difference between revisions
Jump to navigation
Jump to search
Tip:CHandles are considerably slower to access than pointers. If you do create any pointers, and are supporting saved games, remember to repopulate them in
TomEdwards (talk | contribs) (New page: {{toc-right}} '''CHandle''' is a C++ class that represents a 32-bit ID (entindex + serial number) unique to every past and present entity in a game. It is used to refer to entities where ...) |
TomEdwards (talk | contribs) (CHandles are slower than pointers) |
||
| Line 2: | Line 2: | ||
'''CHandle''' is a C++ class that represents a 32-bit ID (entindex + serial number) unique to every past and present entity in a game. It is used to refer to entities where [[pointer]]s and [[entity index]]es are unsafe; mainly across the client/server divide and after restoring a saved game. | '''CHandle''' is a C++ class that represents a 32-bit ID (entindex + serial number) unique to every past and present entity in a game. It is used to refer to entities where [[pointer]]s and [[entity index]]es are unsafe; mainly across the client/server divide and after restoring a saved game. | ||
{{tip|CHandles are considerably slower to access than pointers. If you do create any pointers, and are supporting saved games, remember to repopulate them in <code>[[Restore()]]</code>!}} | |||
== Usage == | == Usage == | ||
Revision as of 10:48, 9 June 2009
CHandle is a C++ class that represents a 32-bit ID (entindex + serial number) unique to every past and present entity in a game. It is used to refer to entities where pointers and entity indexes are unsafe; mainly across the client/server divide and after restoring a saved game.
Restore()!Usage
CHandle<YourStoredClass> hMyHandle;
A CHandle is broadly similar to a pointer, but as it is a standard C++ class extra syntax is needed in some places:
- You must assign a pointer to an object, not the object itself
Get()or a cast is required access the class it is storingIsValid()is required to check whether a value is stored- To invalidate, call
Term()or assignINVALID_EHANDLE_INDEX.
EHANDLE
CHandle is often used through the EHANDLE typedef, which is shorthand for CHandle<C_BaseEntity>. If you want to store a particular class it's better to declare a more specific CHandle manually (see previous section).
Example
// Create a pointer to an entity
CBasePlayer* pPlayer = gEntList.FindEntityByClassname( NULL, "player" );
// Store it (twice, for demonstration purposes)
EHANDLE hPlayer = pPlayer;
CHandle<CBasePlayer> hPlayer_ = pPlayer;
// Convert the EHANDLE back to a CBasePlayer pointer
pPlayer = dynamic_cast<CBasePlayer*>( hPlayer.Get() );