GetClosestPlayer - Help
#1

Hi all! I want to make a function but when i use it the return is my id. How can i pass my id from this loop? [ Rep++; ]

pawn Код:
GetClosestPlayer( toplayer, Float:range )
{
    new Float:tpos[ 3 ], targetid = INVALID_PLAYER_ID;
    GetPlayerPos( toplayer, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] );
    foreach( Player, i )
    {
        if( IsPlayerInRangeOfPoint( i, range, tpos[ 0 ], tpos[ 1 ], tpos[ 2 ] ) )
            targetid = i;
    }
    return targetid;
}
Reply
#2

pawn Код:
stock GetClosestPlayer( playerid, Float:range )
{
    new Float:x, Float:y, Float:z; GetPlayerPos( playerid, x, y, z );
    foreach( Player, i )
    {
        if( !IsPlayerInRangeOfPoint(i, range, x, y, z)) continue;
        else if( playerid == i) continue;
        else return i;
     }
     return -1;
}
Reply
#3

Why did u write: foreach( new i:Player ) instead of foreach( Player, i ) ? xD
Reply
#4

That function does not return the closest player at all.
Reply
#5

Fixed a few functions' arguments whose order I forgot.. I don't use foreach, frankly, I don't need it, so messed that up.
Reply
#6

Yeah i see...
Function didn't work xD
Reply
#7

Not sure if this will work. Only tested for compile errors.

Bugged code removed. Derp.
Reply
#8

I wrote this for you. Includes a 'range' parameter:

pawn Код:
stock GetClosestPlayer(playerid, Float:range = FLOAT_INFINITY)
{
    // Get the target player's current position
    new Float:playerPos[3];
    GetPlayerPos(playerid, playerPos[0], playerPos[1], playerPos[2]);
   
    // Create a float variable to store the closest player distance
    new Float:closestDist = FLOAT_INFINITY;
    new closestPlayer = INVALID_PLAYER_ID;
   
    new Float:thisDist;
    foreach(new i : Player)
    {
        if(i == playerid) continue; // We don't want to include playerid themselves!
       
        thisDist = GetPlayerDistanceFromPoint(i, playerPos[0], playerPos[1], playerPos[2]);
        if(thisDist < closestDist && thisDist < range) // This player is closer than the previous 'closest' player
        {
            closestPlayer = i;
            closestDist = thisDist;
        }
    }
    return closestPlayer;
}
Returns INVALID_PLAYER_ID if no players in range or found.

Not tested, sorry. Looks fine to me.

If you don't have FLOAT_INFINITY defined:

pawn Код:
#define FLOAT_INFINITY (Float:0x7F800000)
P.S. None of you forgot to omit 'playerid' themselves from the loop, meaning that playerid is always going to be closest, as the distance to yourself is well... 0.
Reply
#9

I tested it with 2 bots on my test server and i think it works fine. I will release it on my server soon. I want to use it for /ar (to arrest a player xD)

Thx a lot! Also if u can include a check if players have the save virtual world...

REP++;
Reply
#10

Well, MP2's worked fine, I see, my function would fail if there were two guys within the range and would return the lower ID'd guy without looping through the rest !
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)