SA-MP Forums Archive
Checking if player is near a list of positions? - 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: Checking if player is near a list of positions? (/showthread.php?tid=613209)



Checking if player is near a list of positions? - GunZsmd - 25.07.2016

Hi guys,
So i was wondering if instead of using IsPlayerInRangeOfPoint(playerid, (range), (x,y,z)) we could probably do a list of positions and check if the player is near any of them? Can i do it? and if i can how?


Re: Checking if player is near a list of positions? - Threshold - 25.07.2016

Yes, but the approach to this depends on whether these positions are going to change while the script is running or not. Are you adding the positions in-game or do you add them manually to the script?

Anyway, using arrays, here's an example.
PHP код:
new Float:MyPoints[][3] = { // These are all your coordinates. {x coordinate, y coordinate, z coordinate}
{100.0200.0300.0}, // Point 1
{1000.02000.03000.0}, // Point 2
{10.020.030.0}, // Point 3
{1.02.03.0// Point 4
// You can continue this as many times as you like
};
IsPlayerWithinRangeOfPoint(playeridFloat:rangepoint = -1)
{
    if(
point == -1// If you aren't trying to find a particular point, but all points in general
    
{
        for(new 
0sizeof(MyPoints); i++) // Start looping through all the points you have created.
        
{
            if(
IsPlayerInRangeOfPoint(playeridrangeMyPoints[i][0], MyPoints[i][1], MyPoints[i][2])) // If the player is within range of the point...
                
return 1// Return '1' or true.
        
}
    }
    else if(
<= point <= sizeof(MyPoints)) // If you are trying to check for a particular point, for example point 3
    
{
        return 
IsPlayerInRangeOfPoint(playeridrangeMyPoints[point][0], MyPoints[point][1], MyPoints[point][2]); // Checks if they are near the point specified, returns either 1 or 0.
    
}
    return 
0// Return '0' or false. They are not near a point.

Here's an example of how you might use it. To check if a player is within 10.0 units of any given point:
PHP код:
public OnPlayerSpawn(playerid)
{
    if(
IsPlayerWithinRangeOfPoint(playerid10.0))
    {
        
SendClientMessage(playerid, -1"You are within 10 metres of a point!");
    }
    else 
SendClientMessage(playerid, -1"You are not close enough to a point.");
    return 
1;

If you wanted to see if they were within 100 units of point 12, you would do this:
PHP код:
public OnPlayerSpawn(playerid)
{
    if(
IsPlayerWithinRangeOfPoint(playerid100.012))
    {
        
SendClientMessage(playerid, -1"You are within 100 metres of point number 12!");
    }
    else 
SendClientMessage(playerid, -1"You are not close enough to point number 12.");
    return 
1;




Re: Checking if player is near a list of positions? - GunZsmd - 25.07.2016

Thanks man apreciate it