Get closest vehicle.
#1

Good day everyone. I have a problem with this function, why does it returns 0 always?

pawn Код:
stock GetClosestVehicle(playerid)
{

    new
        Float: finalDistance = 5.0,
        Float: userPos[3],
        Float: Distance,
       
        Closest = INVALID_VEHICLE_ID
    ;
   
    GetPlayerPos(playerid, userPos[0], userPos[1], userPos[2]);

    for (new i; i != MAX_VEHICLES; ++i)
    {
        if ((Distance = GetVehicleDistanceFromPoint(i, userPos[0], userPos[1], userPos[2])) < finalDistance)
        {
            finalDistance = Distance;
            Closest = i;
        }
    }
   
    printf("Closest: %d", Closest);
   
    return Closest;
}
Reply
#2

Because your radius is 5.0 and if the vehicle is more far away than 5.0 GTA units the result will be 0.
Reply
#3

So, everything is okay with the function, but I need to increase distance? Can't test it now, watching basketball ;3
Reply
#4

Try this

pawn Код:
stock GetNearestVehicle(playerid, Float:Distance = 1000.0)
{
    if(!Distance) Distance = 1000.0;
    new Float:X[2], Float:Y[2], Float:Z[2], Float:NearestPos = floatabs(Distance), NearestVehicle = INVALID_VEHICLE_ID;
    GetPlayerPos(playerid, X[0], Y[0], Z[0]);
    for(new i; i < MAX_VEHICLES; i++)
    {
        if(!IsVehicleStreamedIn(i, playerid) || IsPlayerInVehicle(playerid, i)) continue;
        GetVehiclePos(i, X[1], Y[1], Z[1]);
        if(NearestPos > GetDistanceBetweenPoints(X[0], Y[0], Z[0], X[1], Y[1], Z[1])) NearestPos = GetDistanceBetweenPoints(X[0], Y[0], Z[0], X[1], Y[1], Z[1]), NearestVehicle = i;
    }
    return NearestVehicle;
}
Don't forgot +Rep
Reply
#5

@MouseBreaker Nice copying & paste then asking for rep huh? /facepalm Just help him on his code (even if its not his own).

OT: Yes increase the the radius value.
Reply
#6

Still. Its something wrong with the code. Because it returns 65535 (INVALID_VEHICLE_ID) if there arent any vehicles near, and if there is one, it returns 0.
Reply
#7

Try this:

pawn Код:
stock GetClosestVehicle(playerid)
{
    new
        iClosestID = INVALID_VEHICLE_ID,
        Float: fFinalDistance,
        Float: fDistance,
        Float: fX,
        Float: fY,
        Float: fZ;
    GetPlayerPos(playerid, fX, fY, fZ);
    fFinalDistance = 1000.0;

    for(new i; i != MAX_VEHICLES; i++)
    {
        if((fDistance = GetVehicleDistanceFromPoint(i, fX, fY, fZ)) < fFinalDistance)
        {
            fFinalDistance = fDistance;
            iClosestID = i;
        }
    }
    return iClosestID;
}
Reply
#8

Same.
Reply
#9

Maybe you're using it wrongly, that's working fine for me...
Reply
#10

pawn Код:
new closestVehicle = GetClosestVehicle(playerid);
               
                if (closestVehicle == INVALID_VEHICLE_ID || GetPlayerState(playerid) != PLAYER_STATE_ONFOOT)
                    return SendClientMessage(playerid, COLOR_WHITE, "[ {ff0000}klaida {ffffff}]: Jыs neesate prie jokios tr. priemonлs!");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)