GetClosestPlayers? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: GetClosestPlayers? (
/showthread.php?tid=369724)
GetClosestPlayers? -
Admigo - 17.08.2012
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
Re: GetClosestPlayers? -
Vince - 17.08.2012
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.
Re: GetClosestPlayers? -
Admigo - 17.08.2012
Thats why i am posting the problem.
Re: GetClosestPlayers? -
Mauzen - 18.08.2012
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.