GetClosestVehicle(...) is not working
#1

I found this cool stock that lets you get the ID of any vehicle that is within a radius of the player

Код:
stock GetClosestVehicle(playerid, Float: fRadius) // by RyDeR`
{
    new
        iClosestID = INVALID_VEHICLE_ID,
        Float: fFinalDistance,
        Float: fDistance,
        Float: fX,
        Float: fY,
        Float: fZ;
    GetPlayerPos(playerid, fX, fY, fZ);
    fFinalDistance = fRadius;
 
    for(new i; i != MAX_VEHICLES; i++)
    {
        if((fDistance = GetVehicleDistanceFromPoint(i, fX, fY, fZ)) < fFinalDistance)
        {
            fFinalDistance = fDistance;
            iClosestID = i;
        }
    }
    return iClosestID;
}
Now I have some rental cars that I want the players to be able to lock:

Код:
CMD:lockrent(playerid, params[])
{
	new str[128];
	new vehicle = GetClosestVehicle(playerid, 20);
	format(str, sizeof(str), "VehicleID: %d   RentedCarId: %d ",vehicle, PlayerInfo[playerid][rentCarNo]);
	SendClientMessage(playerid, COLOR_DARKCORAL, str);//THIS IS FOR TESTING




	if(PlayerInfo[playerid][rentCarNo] != vehicle)
	{
		SendClientMessage(playerid, COLOR_DARKCORAL,"[BS] {AFAFAF}You don't have any vehicles nearby.");
	}
	else if(PlayerInfo[playerid][lockedRentCar] == 0 && PlayerInfo[playerid][rentCarNo] == vehicle)
	{
	    SetVehicleParamsForPlayer(vehicle, playerid, 0, 1);
	    SendClientMessage(playerid, COLOR_DARKCORAL,"[BS] {AFAFAF}You've locked your rental car.");
	}
	else if(PlayerInfo[playerid][lockedRentCar] == 1 && PlayerInfo[playerid][rentCarNo] == vehicle)
	{
		SetVehicleParamsForPlayer(vehicle, playerid, 0, 0);
		SendClientMessage(playerid, COLOR_DARKCORAL,"[BS] {AFAFAF}You've unlocked your rental car.");
	}
	
	return 1;
}
My code is working fine, but the stock code always gives me vehicle IDs of 0. I have a problem when I use the /lockrent and at RentedCarID I get the correct value, but at Vehicle I always get 0. So the closest vehicle is always with ID: 0, whereas It should be 7 at my testing case.

RentCarNo is the vehicle ID of the car that is being rented (is working correctly)
lockedRentCar - 0=unlocked 1=locked
vehicle - the vehicle ID of the closest car (not worknig correctly, always shows ID: 0)
Reply
#2

Vehicles ID start from 1.
pawn Код:
for(new i = 1; i != MAX_VEHICLES; i++)
Reply
#3

Код:
stock GetClosestVehicle(playerid)
{
	new
		Float:fPos[3],
		Float:distance = 30,
		Float:curdistance,
		currentVehicle;
	
	for(new v = 0; v < MAX_VEHICLES; v++)
	{
		GetVehiclePos(v, fPos[0], fPos[1], fPos[2]);		
		curdistance = GetPlayerDistanceFromPoint(playerid, fPos[0], fPos[1], fPos[2]);
		
		if(curdistance < distance)
		{
			currentVehicle = v;
			distance = curdistance;
		}
	}
	return currentVehicle;
}
I used this stock in my previous script, found it online. This doesn't have a range but you can specify it as fixed if you want.. I'm still learning but trying to help you as best as i can!
Reply
#4

Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
Vehicles ID start from 1.
pawn Код:
for(new i = 1; i != MAX_VEHICLES; i++)
This is my new for loop and it's still not working, I get 17 instead of 0 now. I have a total of 16 vehicles placed on my server
Код:
for(new i = 1; i < MAX_VEHICLES; i++)
Reply
#5

Quote:
Originally Posted by Sibuscus
Посмотреть сообщение
This is my new for loop and it's still not working, I get 17 instead of 0 now. I have a total of 16 vehicles placed on my server
Код:
for(new i = 1; i < MAX_VEHICLES; i++)
As I said, vehicles IDs start from 1. This means, 16 vehicles with IDs starting from 1 to 17. There is no vehicle with ID 0.

You can use /dl in-game for testing purposes to see if the closest vehicle printed is the one closer to you.
Reply
#6

I made func like that too you can try:

PHP код:
GetClosestVehicle(playerid)
{
    new 
Float:vxFloat:vyFloat:vzlast_vehicle = -1Float:last_distance 99999;
    for(new 
vi 1vj GetVehiclePoolSize(); vi <= vjvi++)
    {
        
GetVehiclePos(vivxvyvz);
        if(
vi == INVALID_VEHICLE_ID || GetPlayerDistanceFromPoint(playeridvxvyvz) > last_distance) continue;
        
last_vehicle vi;
        
last_distance GetPlayerDistanceFromPoint(playeridvxvyvz);
    }
    return 
last_vehicle;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)