SA-MP Forums Archive
GetClosestVehicle problem - 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: GetClosestVehicle problem (/showthread.php?tid=294955)



GetClosestVehicle problem - Vukilore - 04.11.2011

Hello guy !
What's the problem with my code ?
When I type the command near the vehicule (id 583) it says i'm not enough close to that vehicle

Here the command
pawn Код:
COMMAND:takekey(playerid, params[])
{
     
        new carid = GetClosestVehicle(carid);
        if(carid == 583)
        {
            SendClientMessage(playerid, 0x80FF00FF, "You take the key of the vehicle.");
         }
        else
        {
            SendClientMessage(playerid, 0x80FF00FF, "You are not enough close to the vehicle.");
        }
        return 1;
}
And Here the function GetClosest vehicle:

pawn Код:
forward GetClosestVehicle(carid);
public GetClosestVehicle(carid)
{
    new x,Float:dis,Float:dis2,car;
    car = 0;
    dis = 99999.99;
    for ( x = 0; x < MAX_VEHICLES; x++ )
    {
        if(x != carid)
        {
            dis2 = GetDistanceBetweenVehicles(x,carid);
            if(dis2 < dis && dis2 < 8.0)
            {
                dis = dis2;
                car = x;
            }
        }
    }
    return car;
}
forward Float:GetDistanceBetweenVehicles(vehicleid,carid);
public Float:GetDistanceBetweenVehicles(vehicleid,carid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    GetVehiclePos(vehicleid,x1,y1,z1);
    GetVehiclePos(carid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
Thank's alot and sorry for bad english


Re: GetClosestVehicle problem - [MG]Dimi - 04.11.2011

Can you explain me more? Carid as param is vehicle from which you want to get closest vehicle?


Re : GetClosestVehicle problem - Vukilore - 04.11.2011

Yeah, i want:
If the player is close to vehicle 583 he can have a key


Re: GetClosestVehicle problem - [MG]Dimi - 04.11.2011

Still you make confusion. Look:
PHP код:
new carid GetClosestVehicle(carid); 
Here you are calling GetClosestVehicle for carid which actually doesn't exist. You need to get closest vehicle from player and you are taking it from carid which, as I said, doesn't exist. Get Player's co-ordinates and then make it so it get's closest vehicle from those x,y,z.


Re: GetClosestVehicle problem - RazR_ - 04.11.2011

Try this:

pawn Код:
GetClosestVehicleForPlayer(playerid)
{
    new vehicleid = INVALID_VEHICLE_ID;
    new Float:distance = 99999.0;
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
    for(i = 0; i < MAX_VEHICLES; i++)
    {
        if(GetVehicleDistanceFromPoint(i, X, Y, Z) < distance && distance < 8.0) //you can change this value
        {
            vehicleid = i;
            distance = GetVehicleDistanceFromPoint(i, X, Y, Z);
        }
    }
    return vehicleid;
}
pawn Код:
COMMAND:takekey(playerid, params[])
{
     
        new carid = GetClosestVehicleForPlayer(playerid);
        if(carid == 583)
        {
            SendClientMessage(playerid, 0x80FF00FF, "You take the key of the vehicle.");
        }
        else
        {
            SendClientMessage(playerid, 0x80FF00FF, "You are not enough close to the vehicle.");
        }
        return 1;
}



Re : GetClosestVehicle problem - Vukilore - 04.11.2011

doesn't work
But thanks


Re: GetClosestVehicle problem - SuperViper - 04.11.2011

Car IDs are different from model IDs. Try:

pawn Код:
COMMAND:takekey(playerid, params[])
{
     
        new carid = GetClosestVehicle(playerid);
        if(GetVehicleModel(carid) == 583)
        {
            SendClientMessage(playerid, 0x80FF00FF, "You take the key of the vehicle.");
        }
        else
        {
            SendClientMessage(playerid, 0x80FF00FF, "You are not enough close to the vehicle.");
        }
        return 1;
}



Re : GetClosestVehicle problem - Vukilore - 04.11.2011

Oh god, its so simply xD, i'll try thanks you


Re : GetClosestVehicle problem - Vukilore - 04.11.2011

But its not work


Re : GetClosestVehicle problem - Vukilore - 05.11.2011

Any others help please ?