Issues with a function.
#1

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.
Reply
#2

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

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

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;
}
Reply
#5

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)