UTIL PlayerByIndex: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
CBasePlayer *UTIL_PlayerByIndex( int entindex ) { return ToBasePlayer( ClientEntityList().GetEnt( entindex ) );  
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.


Players entities range from 1 - MAXPLAYERS, and non-players entities start at MAXPLAYERS + 1. I.E if A joins (1), then B joins (2) then A leaves 1 will free up, Player C joins (1)
== Shared declaration: ==
<source lang=cpp>
CBasePlayer *UTIL_PlayerByIndex( int entindex );
</source>


(much thanks to DaFox for providing this information).
== Example usage: ==
 
The following code can be used to savely iterate all player entities:
gpGlobals->maxClients will return the number of connected clients.
<source lang=cpp>
 
for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{{stub}}
{
{{todo|Examples?}}
CBasePlayer *pPlayer = UTIL_PlayerByIndex(i);
if (!pPlayer || !pPlayer->IsPlayer())
continue;
[...]
}
</source>


[[Category:Programming]]
[[Category:Programming]]
[[Category:UTIL]]
[[Category:UTIL]]

Revision as of 17:43, 10 April 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.

Shared declaration:

CBasePlayer *UTIL_PlayerByIndex( int entindex );

Example usage:

The following code can be used to savely iterate all player entities:

for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
	CBasePlayer *pPlayer = UTIL_PlayerByIndex(i);
	if (!pPlayer || !pPlayer->IsPlayer())
		continue;
	[...]
}