UTIL PlayerByIndex: Difference between revisions
Jump to navigation
Jump to search
(Created page with 'CBasePlayer *UTIL_PlayerByIndex( int entindex ) { return ToBasePlayer( ClientEntityList().GetEnt( entindex ) ); Players entities range from 1 - MAXPLAYERS, and non-players enti…') |
mNo edit summary |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:UTIL_PlayerByIndex}} | |||
This function will return a [[CBasePlayer]] [[pointer]] to an [[entity]] based on the networked entity index which is passed in as a first parameter. The [[edict]] indices in the range 1 to gpGlobals->maxClients are reserved for players; all networked, non-player entities will be assigned to an index higher than gpGlobals->maxClients. | |||
== Usage == | |||
<source lang=cpp> | |||
CBasePlayer *UTIL_PlayerByIndex( int entindex ); | |||
</source> | |||
== Examples == | |||
The following code can be used to safely iterate all player entities: | |||
<source lang=cpp> | |||
for ( int i = 1; i <= gpGlobals->maxClients; i++ ) | |||
{ | |||
CBasePlayer *pPlayer = UTIL_PlayerByIndex(i); | |||
if (!pPlayer || !pPlayer->IsPlayer()) | |||
continue; | |||
[...] | |||
} | |||
</source> | |||
[[Category:Programming]] | |||
[[Category:UTIL]] |
Latest revision as of 00:27, 5 December 2011
This function will return a CBasePlayer pointer to an entity based on the networked entity index which is passed in as a first parameter. The edict indices in the range 1 to gpGlobals->maxClients are reserved for players; all networked, non-player entities will be assigned to an index higher than gpGlobals->maxClients.
Usage
CBasePlayer *UTIL_PlayerByIndex( int entindex );
Examples
The following code can be used to safely iterate all player entities:
for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex(i);
if (!pPlayer || !pPlayer->IsPlayer())
continue;
[...]
}