GetClosestVehicle not working
#1

Hey. I'm using GetClosestVehicle for my server, but it isn't working.
I've tried the one from uf.inc, here https://sampwiki.blast.hk/wiki/Useful_Functions, and my own version, here:
pawn Код:
stock GetClosestVehicle(playerid)
{
    new closestdist = 0, closestvehicle = -1;
    new Float:x[2],Float:y[2],Float:z[2],distance;
    for(new i; i < MAX_VEHICLES; i++)
    {
        GetVehiclePos(i,x[0],y[0],z[0]);
        //if(x[0] == 0.0 && y[0] == 0.0 && z[0] == 0.0) return -1;
        GetPlayerPos(playerid,x[1],y[1],z[1]);
        distance = floatround(GetDistance(x[0],y[0],z[0],x[1],y[1],z[1]));
        if(distance < closestdist) {
            closestdist = distance;
            closestvehicle = i;
        }
    }
    return closestvehicle;
}

I have made a command, /closestcar:
pawn Код:
CMD:closestcar(playerid, params[]) {
    #pragma unused params
    new closest = GetClosestVehicle(playerid);
    if(closest != -1) {
        PutPlayerInVehicle(playerid, closest, 0);
        SendFormattedMessage(playerid, COLOR_WHITE, "You have been put in vehicle ID: %d", closest);
    }
    return 1;
}
But nothing happens. If I remove the if-statement check, it says "You have been put in vehicle ID: -1", which shouldn't happen as there are vehicles on my server.
Reply
#2

pawn Код:
new closestdist = 0,
// and
        if(distance < closestdist) {
How is it supposed to ever be less than 0?

Change your initital value to:

pawn Код:
new closestdist = 0x7FFFFFFF;
Reply
#3

Yeah I actually noticed my mistake just after posting this topic *embarrassed*

Thanks anyway, I just redefined it as 999999999 but I will use yours.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)