bigguy | Useful UTIL functions |
Useful UTIL functions for casting CBasePlayer pointers
Red = code
Green = comment
Tan = File name
Add these declarations to util.h:
// bigguyAdd this to util.cpp:
// bigguy CBasePlayer* UTIL_CastPlayer ( CBaseEntity *pEnt ) { if (pEnt) // check validity { if (pEnt->IsPlayer()) // is it even a player? { CBasePlayer *plr = GetClassPtr((CBasePlayer *)pEnt); // cast player if (plr) // check validity return plr; // return pointer } } return NULL; // otherwise, its not a player } CBasePlayer* UTIL_CastPlayer ( entvars_t *pev ) { if (pev) // check validity { if (FClassnameIs(pev, "player")) // is it even a player? { CBasePlayer *plr = GetClassPtr((CBasePlayer *)pev); // cast player if (plr) // check validity return plr; // return pointer } } return NULL; // otherwise, its not a player } CBasePlayer* UTIL_CastPlayer ( edict_t *pEdict ) { if (pEdict) // check validity { if (FClassnameIs(pEdict, "player")) // is it even a player? { entvars_t *pev = &pEdict->v; CBasePlayer *plr = GetClassPtr((CBasePlayer *)pev); // cast player if (plr) // check validity return plr; // return pointer } } return NULL; // otherwise, its not a player } CBasePlayer* UTIL_CastPlayer ( int index ) { if (index > 0 && index <= gpGlobals->maxClients) // check validity { edict_t *pEdict = INDEXENT(index); // cast pEdict CBasePlayer *plr = UTIL_CastPlayer(pEdict); // cast player if (plr) // check validity return plr; // return pointer } return NULL; // otherwise, its not a player }
Usage:
If you have a pointer to a CBaseEntity, entvars_t, edict_t or just the index of an entity, you can cast that into a CBasePlayer with these functions. The functions themselves, should handle any NULL pointers you pass into it, and will only return NULL if it cannot cast the pointer/index into a CBasePlayer. You should use the functions like this:CBasePlayer*pPlayer = UTIL_CastPlayer ( [pointer/index] ); if (pPlayer) ...