Local chat alternative (IsPlayerInRangeOfPoint())
#1

I'm currently working on a SAMP Roleplay script. I'm trying to build it from scratch, just to get familiar with the Pawn-language and stuff. When creating a local chat function, I did some searches to see how it's usually done. I found this:

Quote:
Originally Posted by SilentHuntR
Посмотреть сообщение
pawn Код:
stock SendLocalChat(playerid,color,msg[],Float:radius)
{
  new Float:x,Float:y,Float:z;
  GetPlayerPos(playerid,x,y,z);
  for(new ply;ply<MAX_PLAYERS;ply++)
  {
    if(IsPlayerInRangeOfPoint(ply,radius,x,y,z))SendClientMessage(ply,color,msg);
  }
  return 1;
}
But... wouldn't it be much better to keep track of the highest player id in the server, and implement this in the IsPlayerInRangeOfPoint function? This would be a huge performance boost. Best case scenario is one step instead of 500. This is how I did it:

pawn Код:
new Hid = -1; // Hid stores the largest known playerid, value -1 when 0 players

public OnPlayerConnect(playerid) {
    if (playerid > Hid) Hid = playerid; // Updating highest playerid
    return 1;
}

public OnPlayerDisconnect(playerid, reason) {
    // When player with playerid hid leaves, update hid value //
    if (playerid == Hid) {
        while (Hid > -1) {
            Hid--;
            if(!(Hid == INVALID_PLAYER_ID)) break;
        }
    }
    return 1;
}
Then...

pawn Код:
for (new ply; ply<(Hid+1); ply++) {
        if (IsPlayerInRangeOfPoint(bla)) function();
}
Reply
#2

There could be some IDs between 0 and highest ID that are not connected.

For example there could be players with IDs 0, 1, 5, 6, 10.

And the performance you get from there isn't very big.

You can always redefine MAX_PLAYERS also.
Reply
#3

Just change MAX_PLAYERS to number of your slots or use foreach
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)