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; }
First, check if the vehicle ID is valid using GetVehicleModel. Second, show me the GetVehicleDistanceFromPoint function.
|
The GetVehicleDistanceFromPoint function is a native samp function. The problem is the car ID, that it always shows me that the ID is 1999
|
TIL.
It always show you the ID is 1999 because the loop runs until the end. The code structure goods look though, what range are you using? And, you're near 0.0, 0.0, 0.0 in Blueberry? |
GetClosestVehicle(playerid, Float:range) { new Float:POS[3]; if(GetPlayerPos(playerid, POS[0], POS[1], POS[2])) // check is player valid + get player pos { new Float:distance, cveh; for(new v; v < MAX_VEHICLES; v++) { if(!IsValidVehicle(v)) return INVALID_VEHICLE_ID; distance = GetVehicleDistanceFromPoint(v, POS[0], POS[1], POS[2]); if(distance <= range) cveh = v; } return cveh; } return false; // smthng wrong }
Код HTML:
GetClosestVehicle(playerid, Float:range) { new Float:POS[3]; if(GetPlayerPos(playerid, POS[0], POS[1], POS[2])) // check is player valid + get player pos { new Float:distance, cveh; for(new v; v < MAX_VEHICLES; v++) { if(!IsValidVehicle(v)) return INVALID_VEHICLE_ID; distance = GetVehicleDistanceFromPoint(v, POS[0], POS[1], POS[2]); if(distance <= range) cveh = v; } return cveh; } return false; // smthng wrong } |
When I put it on a command it returns me Server:Unknown command.
Console: ![]() |
https://sampwiki.blast.hk/wiki/IsValidVehicle
IVV isnt defined by default xd define it like in wiki page xd |
#define LoopVehicles(%1) \
for(new %1 = GetVehiclePoolSize(); i != %1; %1--)
stock GetClosestVehicle(playerid, Float:range)
{
new Float: p_X, Float: p_Y, Float: p_Z, Float: Distance = 9999.9, Vehicle = INVALID_VEHICLE_ID, Float: Temp;
GetPlayerPos(playerid, p_X, p_Y, p_Z);
LoopVehicles(i)
{
Temp = GetVehicleDistanceFromPoint(i, p_X, p_Y, p_Z);
if(range <= Temp <= Distance) //if(Temp <= Distance && Temp <= range)
{
Vehicle = i;
Distance = Temp;
}
}
return Vehicle;
}