SA-MP Forums Archive
Issues with a function. - 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: Issues with a function. (/showthread.php?tid=382075)



Issues with a function. - vIBIENNYx - 01.10.2012

The function is used to get the closest vehicle to a player, however, it doesn't work as expected. It doesn't check the vehicles as expected.

Here is the code:
pawn Код:
forward GetClosestVehicleFromPlayer(playerid);
public GetClosestVehicleFromPlayer(playerid)
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetVehicleModel(playerid) != 0)
        {
            new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
            if(distance < 3)
            {
                return i;
            }
        }
    }
    return -1;
}
Here is the exact issue. I tried the function before a vehicle was spawned and it worked correctly, however. Once a vehicle is spawn it is as if the for loop doesn't check the ID's properly. Here is a screenshot.



Any ideas?

Edit: For Player ID 0, the Returning ID is -1, for Player ID 1, the the ID returns 0.


Re: Issues with a function. - Vince - 01.10.2012

pawn Код:
if(GetVehicleModel(playerid) != 0)



Re: Issues with a function. - vIBIENNYx - 01.10.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
pawn Код:
if(GetVehicleModel(playerid) != 0)
That basically checks to see if the vehicle exists.


Re: Issues with a function. - ReneG - 02.10.2012

That would check if a player was in a car, or if the model id of the car the player is in is not 0.

It should be
pawn Код:
public GetClosestVehicleFromPlayer(playerid)
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(IsValidVehicle(i))
        {
            new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
            if(distance < 3)
            {
                return i;
            }
        }
    }
    return -1;
}



Re: Issues with a function. - vIBIENNYx - 02.10.2012

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
That would check if a player was in a car, or if the model id of the car the player is in is not 0.

It should be
pawn Код:
public GetClosestVehicleFromPlayer(playerid)
{
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(IsValidVehicle(i))
        {
            new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
            if(distance < 3)
            {
                return i;
            }
        }
    }
    return -1;
}
Thank you very much. This has solved the issue, repped you both.