How to detect if a player is in range of another player?
#1

Hello. I've tried looping through all players, detecting if I'm in-range of anyone. But upon doing that, it gives EVERYONE a fish. I'm currently creating a "give" option via dialog, and want it to give the nearest-player a fish.

How do I detect if a player is near another player? Is there a function out there somewhere similar to this:

pawn Код:
IsPlayerInRangeOfPlayer(playerid, Float:distance, player2)
Thanks for any help.
Reply
#2

You want this?
Quote:

new Float:X, Float:Y, Float:Z;
GetPlayerPos(otherplayerid,X,Y,Z);
if(IsPlayerInRangePoint(playerid,10,X,Y,Z);
{
your code
}

Reply
#3

Whatever works, yes.

Thank you. Would also be cool if you could explain the code to me so i could learn.
Reply
#4

if(GetDistanceBetweenPlayers(playerid,otherplayer) < x) // x should be something like 10 ; 10 meters ..
Reply
#5

Okay, so you could make a stock for this if you want to do it like this:

Код:
stock IsPlayerInRangeOfPlayer(playerid, Float: range, player2)
{
if(!IsPlayerConnected(player2)) return 0;
new Float: x, Float: y, Float: z;
GetPlayerPos(playerid, x, y, z);
if(IsPlayerInRangeOfPoint(player2, range, x, y, z) return 1;
else if(!IsPlayerInRangeOfPoint(player2, range, x, y, z) return 0;
}
So what it does is use x, y, z to detect the first player's position. Then if they are in range of that position you'll return 1 if not just return 0.

An example of how you can use this is:

Detecting if they ARE in range: if(IsPlayerInRangeOfPlayer(playerid, 25.0, player2);
if they're not... if(!IsPlayerInRangeOfPlayer... Bla bla bla

Hope I explained this throughly enough!
Reply
#6

Quote:
Originally Posted by Abagail
Посмотреть сообщение
Код:
if(IsPlayerInRangeOfPoint(player2, range, x, y, z) return 1;
else if(!IsPlayerInRangeOfPoint(player2, range, x, y, z) return 0;
You can use just

pawn Код:
return IsPlayerInRangeOfPoint(player2, range, x, y, z);
Reply
#7

@Jeff, so true. Thanks for informing me about it. Wasn't thinkin' bout' that.
Reply
#8

Thanks for the help. I modified the stock in order to remove a few errors. But upon using it, i get a error saying "player2" isn't defined.

EDIT: I'm trying to make it automatically detect the closest player.

pawn Код:
stock IsPlayerInRangeOfPlayer(playerid, Float: range, player2)
{
    if(!IsPlayerConnected(player2)) return 0;
    new Float: x, Float: y, Float: z;
    GetPlayerPos(playerid, x, y, z);
    if(IsPlayerInRangeOfPoint(player2, range, x, y, z)) return 1;
    else if(!IsPlayerInRangeOfPoint(player2, range, x, y, z)) return 0;
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)