SA-MP Forums Archive
GetClosestVehicle not working - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: GetClosestVehicle not working (/showthread.php?tid=267442)



GetClosestVehicle not working - jameskmonger - 08.07.2011

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.


Re: GetClosestVehicle not working - Vince - 08.07.2011

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;



Re: GetClosestVehicle not working - jameskmonger - 08.07.2011

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.