getClosestVehicle
GET_CLOSEST_VEHICLE0xF73EB622C4F1689BThe GTA V native GET_CLOSEST_VEHICLE of the VEHICLE namespace, as LS:MP client scripts call it: the global function getClosestVehicle, in JavaScript and in Lua.
getClosestVehicle(
x: number,
y: number,
z: number,
radius: number,
modelHash: Hash,
flags: number
): VehiclegetClosestVehicle(
x: number,
y: number,
z: number,
radius: number,
modelHash: Hash,
flags: integer
): VehicleUsage
const vehicle = getClosestVehicle(x, y, z, radius, modelHash, flags);local vehicle = getClosestVehicle(x, y, z, radius, modelHash, flags)Three consecutive numbers can be given as one vector: { x, y, z } (or [x, y, z]) in JavaScript, a Vector3 in Lua.
const vehicle = getClosestVehicle(pos, radius, modelHash, flags);local vehicle = getClosestVehicle(pos, radius, modelHash, flags)Parameters
| Parameter | Type | JavaScript | Lua | Description |
|---|---|---|---|---|
x | float | number | number | Position to get closest vehicle to. |
y | float | number | number | Position to get closest vehicle to. |
z | float | number | number | Position to get closest vehicle to. |
radius | float | number | number | Max radius to get a vehicle. |
modelHash | Hash | number | integer | Limit to vehicles with this model. 0 for any. |
flags | int | number | integer | The bitwise flags altering the function's behaviour. |
Returns
Vehicle JavaScript number, Lua integerGame handle of a vehicle. Not an LS:MP netId. Convert one with lsmp.getEntityFromNetId.
Notes
From the community native database. Names are LS:MP’s; samples may be written for C++ or another framework.
Example usagegetClosestVehicle(x, y, z, radius, hash, unknown leave at 70)
Does not return police cars or helicopters.
It seems to return police cars for me, does not seem to return helicopters, planes or boats for some reason
Only returns non police cars and motorbikes with the flag set to 70 and modelHash to 0. ModelHash seems to always be 0 when not a modelHash in the scripts, as stated above.
These flags were found in the b617d scripts: 0,2,4,6,7,23,127,260,2146,2175,12294,16384,16386,20503,32768,67590,67711,98309,100359.
Converted to binary, each bit probably represents a flag as explained regarding another native here: https://gtaforums.com/topic/822314-guide-driving-styles
Conversion of found flags to binary: https://pastebin.com/kghNFkRi
At exactly 16384 which is 0100000000000000 in binary and 4000 in hexadecimal only planes are returned.
It's probably more convenient to use worldGetAllVehicles(int *arr, int arrSize) and check the shortest distance yourself and sort if you want by checking the vehicle type with for example isThisModelABoat
Conclusion: This native is not worth trying to use. Use something like this instead: https://pastebin.com/xiFdXa7h
Full list of vehicles by DurtyFree: https://github.com/DurtyFree/gta-v-data-dumps/blob/master/vehicles.json
Reference
- Namespace
- Vehicle VEHICLE
- In the game
Vehicle GET_CLOSEST_VEHICLE(float x, float y, float z, float radius, Hash modelHash, int flags)- Hash
0xF73EB622C4F1689B