Getting the highest ID
#5

Quote:
Originally Posted by Amit B
Maybe this will help:
pawn Код:
stock GetHighestID()
{
  new highestID = -1;
  for(new i = 0; i < MAX_PLAYERS; i++) highestID = i;
  return highestID;
}
Return the highest ID, or -1 if there are no highest ID (0 online players).
Example:
pawn Код:
new HighestID = GetHighestID();
That would simply return MAX_PLAYERS - 1
It would have to be:
pawn Код:
stock GetHighestID()
{
  new highestID = -1;
  for(new i = 0; i < MAX_PLAYERS && IsPlayerConnected(i); i++) highestID = i;
  return highestID;
}
Or if that doesn't work:
pawn Код:
stock GetHighestID()
{
  new highestID = -1;
  for(new i = 0; i < MAX_PLAYERS; i++) if(IsPlayerConnected(i)) highestID = i;
  return highestID;
}

EDIT: However, IMO, looping normally using MAX_PLAYERS then checking IsPlayerConnected would be faster than looping with GetHighestID(). Also, you would still need to check IsPlayerConnected with GetHighestID, because player ID's aren't always in order. Eg. if there are 6 players on the server (ID's 0-5), and player 5 (ID 4) leaves, then you have ID's 0,1,2,3,5; 5 is the highest ID, but not all players up to that ID are connected.
Reply


Messages In This Thread
Getting the highest ID - by [LL]InstabiC - 26.09.2009, 08:26
Re: Getting the highest ID - by Zeex - 26.09.2009, 08:43
Re: Getting the highest ID - by Sznupek - 26.09.2009, 08:49
Re: Getting the highest ID - by Amit_B - 26.09.2009, 10:50
Re: Getting the highest ID - by ev0lution - 28.09.2009, 20:59

Forum Jump:


Users browsing this thread: 1 Guest(s)