GetClosestPlayers?
#1

Heey all,

Is it possible to detect more then one(multiple) players that is in your distance like range 10?
Code:
pawn Код:
public GetClosestPlayer(p1)
{
    new Float:dis,Float:dis2,player;
    player = -1;
    dis = 99999.99;
    foreach(Player,x)
    {
        if(x != p1)
        {
            dis2 = GetDistanceBetweenPlayers(x,p1);
            if(dis2 < dis && dis2 != -1.00)
            {
                dis = dis2;
                player = x;
            }
        }
    }
    return player;
}
How can i make that?

Thanks Admigo
Reply
#2

Pass arguments by reference. You could also make use of the ellipsis operator for variable amount of return values. But that might be too advanced for you.
Reply
#3

Thats why i am posting the problem.
Reply
#4

pawn Код:
// Arrays are passed by reference, so if you change a passed array in a function,
// it will also change after returning from it
stock GetClosestPlayers(playerid, Float:range, players[], num = sizeof(players)) {
    new count = 0;
    foreach(Player,x)
    {
        if (x != playerid)
        {
            if (GetDistanceBetweenPlayers(x, playerid) <= range)
            {
                // Add the player to the list of players in range
                // and increase the counter
                players[count++] = x;
            }
        }
        // if the players array is full, return
        // Any possible other players in range will be ignored
        if (count == num) return;
    }
    // if theres still space in the players array after
    // finding all players in range, mark its end
    if (count < num - 1) players[count] = -1;
}
Usage would be like this:
pawn Код:
// The array size defines the max number of players to find
new players[4];
// Find 4 players in the given range
GetClosestPlayers(playerid, 10.0, players);
for (new i = 0; i < sizeof(players); i++) {
    // if reached the marked end of the array, stop processing
    // else there are still 0 in the array and would mess the result
    if (players[i] == -1) break;
    // Do something with the player
}
Untested, but this is a way it could be done. Players in the array arent ordered, so after finding e.g. 4 players and the array is full, there might still be more even closer players, so its no real "GetClosestPlayers". This would be a bit more complex and im too lazy for that right now
Ask again if you really need that, but from your question I think this is enough.

Edit: I really like it when people ask for something, get a short or long answer, and then remain silent forever.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)