GetClosestPlayer returns always 0 ?
#1

Hi

I have this piece of code:

pawn Код:
dcmd_rob(playerid, params[])
{
    #pragma unused params
    if(PlayerInfo[playerid][pTeam] == TEAM_COP) return SendClientMessage(playerid, COLOR_RED, "** Police officers cannot rob other people!");
    new otherplayer = GetClosestPlayer(playerid);
    if(otherplayer == playerid) return SendClientMessage(playerid, COLOR_RED, "** No one close enough to rob!");
   
    SetPlayerCriminal(playerid, 2);
    new attempt = random(2);
    if(attempt == 1)
    {
        // If success, give money etc.
    }
    else
    {
      format(string, sizeof(string), "You attempted to rob some money from %s, but failed!", PlayerName(otherplayer));
        SendClientMessage(playerid, COLOR_YELLOW, string);
    }
    return 1;
}
GetClosestPlayer always seems to return 0 (when I tested I was playerid 0), even when I stand right next to another person.
Does anyone have a working GetClosestPlayer?

I copied my GetClosestPlayer function and the other functions it calls from the uf.inc into a new inc, because I can't include the whole uf.inc without getting a bunch of errors.
Reply
#2

pawn Код:
stock GetClosestPlayer(playerid) //By Slick (edited by Gabriel "Larcius" Cordes)
{
    if(IsPlayerConnected(playerid) && IsPlayerConnected(0))
    {
        new closestplayer=0;
        new Float:closestdist=GetDistanceBetweenPlayers(playerid,0);
        for(new playerid2=0; playerid2<MAX_PLAYERS; playerid2++)
        {
            new Float:dist = GetDistanceBetweenPlayers(playerid,playerid2);
            if ((dist < closestdist))
            {
                closestdist = dist;
                closestplayer = playerid;
            }
        }
        return closestplayer;
    }
    return -1;
}
Lol at the guy who wrote/edited this function.
Reply
#3

Replace your current GetClosestPlayer function with this:

pawn Код:
stock GetClosestPlayer(playerid)
{
  new Float:cdist, targetid = -1;
  for (new i = 0; i < MAX_SLOTS; i++)
  {
    if (IsPlayerConnected(i) && playerid != i && (targetid < 0 || cdist > GetDistanceBetweenPlayers(playerid, i)))
    {
      targetid = i;
      cdist = GetDistanceBetweenPlayers(playerid, i);
    }
  }
  return targetid;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)