SA-MP Forums Archive
Function such as "IsPlayerCloseToPlayer" or something - 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: Function such as "IsPlayerCloseToPlayer" or something (/showthread.php?tid=521510)



Function such as "IsPlayerCloseToPlayer" or something - NoahF - 23.06.2014

Hey

Is there a function that can detect if a player is within a certain distance to another player? I looked all over the SA-MP Wiki but couldn't find one?

Thanks!


Re : Function such as "IsPlayerCloseToPlayer" or something - S4t3K - 23.06.2014

It's impossible you've looked all over the SAMP wiki because there's something which can help you : IsPlayerInRangeOfPoint.

Just get the player2 pos and check if it's close enough through the "radius" parameter to the playerid's position (gotten automatically by SAMP)


Re: Function such as "IsPlayerCloseToPlayer" or something - Clad - 23.06.2014

pawn Код:
stock Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
P.S : by Kostantinos


Re: Function such as "IsPlayerCloseToPlayer" or something - NoahF - 23.06.2014

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
It's impossible you've looked all over the SAMP wiki because there's something which can help you : IsPlayerInRangeOfPoint.

Just get the player2 pos and check if it's close enough through the "radius" parameter to the playerid's position (gotten automatically by SAMP)
Quote:
Originally Posted by Clad
Посмотреть сообщение
pawn Код:
stock Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
P.S : by Kostantinos
This is exactly what I wanted! Thank you. And S4t3K, I saw that but I didn't think that would be useful. Thanks anyways!

EDIT: Now for the GetDistanceBetweenPlayers function, would I do:
Код:
if(GetDistanceBetweenPlayers(playerid, targetid)) = x (x would be a number or a float)



Re : Function such as "IsPlayerCloseToPlayer" or something - S4t3K - 23.06.2014

Konstantinos' function can be optimized using VectorSize :

PHP код:

stock Float
:GetDistanceBetweenPlayers(pl1pl2)
{
      new 
Float:pos[6];
      if(!
IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return -1.0;
      
GetPlayerPos(pl1pos[0], pos[1], pos[2]);
      
GetPlayerPos(pl2pos[3], pos[4], pos[5]);
      return 
VectorSize(pos[0]-pos[3],pos[1] - pos[4], pos[2]-pos[5]);

This "VectorSize" usage is from the wiki page VectorSize, and a similar function called "GetDistanceBetweenPoints" uses this kind of implementation, credits go to Vince.


Re: Function such as "IsPlayerCloseToPlayer" or something - Clad - 23.06.2014

pawn Код:
forward Float:GetDistanceBetweenPlayers(playerid,targetplayerid);

public Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}



Re : Function such as "IsPlayerCloseToPlayer" or something - S4t3K - 23.06.2014

Why do you want to use a public function here ? Except if you plan to call the function through a timer or through CallLocal/RemoteFunction, it's pointless.

And why do you repost the exact function as above ?


Re: Function such as "IsPlayerCloseToPlayer" or something - Rittik - 23.06.2014

Late.


Re: Function such as "IsPlayerCloseToPlayer" or something - NoahF - 23.06.2014

?
Quote:

EDIT: Now for the GetDistanceBetweenPlayers function, would I do:
Code:

if(GetDistanceBetweenPlayers(playerid, targetid)) == x (x would be a number or a float)
I don't understand what you are trying to say by just giving me the stock?


Re: Re : Function such as "IsPlayerCloseToPlayer" or something - Clad - 23.06.2014

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
Why do you want to use a public function here ? Except if you plan to call the function through a timer or through CallLocal/RemoteFunction, it's pointless.

And why do you repost the exact function as above ?
Don't get mad homie, When I sent it I didn't know you will send it too, Relax pls.