GetClosestVehicle
#1

Hi all,

I have the following command:

Код:
stock GetClosestVehicle(playerid, Float:range){
    new Float:p_X;
    new Float:p_Y;
    new Float:p_Z;

    new Float:Distance;
    new Float:PretendentDistance = range +1;
    new Pretendent;

    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    for(new vehicleid=1; vehicleid < MAX_VEHICLES; vehicleid++){
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);

        if(Distance <= range && Distance <= PretendentDistance){
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }

    return Pretendent;
}


new vehicleid = GetClosestVehicle(playerid, 10.0);
However, unfortunately when I print the contents of vehicleid on executing the command right next to a vehicle, it only prints the value of 0, which means this isn't working.

Pulling my hair out with this...any ideas?
Reply
#2

You should start using GetVehiclePoolSize (Unless you're not using SA-MP 0.3.7)
It retrieves the highest vehicle ID, so instead of 2000 (MAX_VEHICLES), you'd be using something much lower if there isn't a lot of vehicles.

I just wrote this, so I'm not sure if it has any bugs or not.
Код:
native IsValidVehicle(vehicleid); // According to the wiki, it doesn't exist in the default sa-mp includes, so you have to add it.

GetClosestVehicle(playerid, Float: range)
{
    new
        vehicleid = -1,
        Float: last = range,
        Float: current,
        
        Float: x, Float: y, Float: z;
    GetPlayerPos(playerid, x, y, z);
    for (new i = 1, j = GetVehiclePoolSize(); i <= j; i++) // for (new i = 1; i < MAX_VEHICLES; i++)
    {
        if (!IsValidVehicle(i)) continue;
        current = GetVehicleDistanceFromPoint(i, x, y, z);
        if (current < last)
        {
            vehicleid = i;
            last = current;
        }
    }
    return vehicleid;
}
Reply
#3

PHP код:
native IsValidVehicle(vehicleid);
GetClosestVehicle(playeridFloat:range) {
   new 
Float:VehiclePos[4], nearestID;
   
nearestID 65535;
   for(new 
vehicleid 0vehicleid MAX_VEHICLESvehicleid++) {
      if(
IsValidVehicle(vehicleid)) {
         
GetVehiclePos(vehicleidVehiclePos[1], VehiclePos[2], VehiclePos[3]);
         if(
IsPlayerInRangeOfPoint(playeridrangeVehiclePos[1], VehiclePos[2], VehiclePos[3])) {
            
nearestID vehicleid;
            break;
         }
      }
   }
   return 
nearestID;

Reply
#4

Quote:
Originally Posted by Stinged
Посмотреть сообщение
You should start using GetVehiclePoolSize (Unless you're not using SA-MP 0.3.7)
It retrieves the highest vehicle ID, so instead of 2000 (MAX_VEHICLES), you'd be using something much lower if there isn't a lot of vehicles.

I just wrote this, so I'm not sure if it has any bugs or not.
Код:
native IsValidVehicle(vehicleid); // According to the wiki, it doesn't exist in the default sa-mp includes, so you have to add it.

GetClosestVehicle(playerid, Float: range)
{
    new
        vehicleid = -1,
        Float: last = range,
        Float: current,
        
        Float: x, Float: y, Float: z;
    GetPlayerPos(playerid, x, y, z);
    for (new i = 1, j = GetVehiclePoolSize(); i <= j; i++) // for (new i = 1; i < MAX_VEHICLES; i++)
    {
        if (!IsValidVehicle(i)) continue;
        current = GetVehicleDistanceFromPoint(i, x, y, z);
        if (current < last)
        {
            vehicleid = i;
            last = current;
        }
    }
    return vehicleid;
}

Thank you, great help. Didn't know about GetVehiclePoolSize, thanks!

Thanks everyone else.
Reply
#5

If enabling camera target (EnablePlayerCameraTarget), retrieving the closest vehicle (GetPlayerCameraTargetVehicle) and then disabling the camera target would be possible, that would be great and we wouldn't even need a loop. Unfortunately, it has to be enabled on connect otherwise it returns INVALID_VEHICLE_ID (just tested it).
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
If enabling camera target (EnablePlayerCameraTarget), retrieving the closest vehicle (GetPlayerCameraTargetVehicle) and then disabling the camera target would be possible, that would be great and we wouldn't even need a loop. Unfortunately, it has to be enabled on connect otherwise it returns INVALID_VEHICLE_ID (just tested it).
That's very interesting actually. I may use this, as I have another system in mind (A type of LSPD dashcam) where this could be quite useful!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)