diff --git a/VEHICLE/GetClosestVehicle.md b/VEHICLE/GetClosestVehicle.md index 3aa52068b..2e83d7307 100644 --- a/VEHICLE/GetClosestVehicle.md +++ b/VEHICLE/GetClosestVehicle.md @@ -8,32 +8,105 @@ ns: VEHICLE Vehicle GET_CLOSEST_VEHICLE(float x, float y, float z, float radius, Hash modelHash, int flags); ``` +Find the closest vehicle to specified coordinates, offering the ability to filter by model and conditions using flags. However, it is not designed to find boats, helicopters, or planes. + +**Note**: It's problably more convenient to use [GET_GAME_POOL](#_0x2B9D4F50) and check the shortest distance yourself and sort if you want by checking the vehicle type with for example [IS_THIS_MODEL_A_BOAT](#_0x45A9187928F4B9E3). The native +only going to return 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. + ``` -Example usage -VEHICLE::GET_CLOSEST_VEHICLE(x, y, z, radius, hash, unknown leave at 70) -x, y, z: Position to get closest vehicle to. -radius: Max radius to get a vehicle. -modelHash: Limit to vehicles with this model. 0 for any. -flags: The bitwise flags altering the function's behaviour. -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: gtaforums.com/topic/822314-guide-driving-styles -Conversion of found flags to binary: 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 VEHICLE::IS_THIS_MODEL_A_BOAT -------------------------------------------------------------------------- Conclusion: This native is not worth trying to use. Use something like this instead: pastebin.com/xiFdXa7h -Use flag 127 to return police cars +``` + +``` +Flags: + - **0**: Returns cars and motorbikes, including those already occupied. Might also return empty vehicles. + - **2**: Returns only empty vehicles, specifically cars and motorbikes. + - **4**: Similar to flag 70, generally effective for cars and motorbikes. + - **23**: Finds cars when the player is not inside a vehicle. + - **70**: Recommended for cars and motorbikes. Does not return occupied vehicles. + - **127**: Does not return vehicles when inside cars or motorbikes. Effective when on foot or inside a helicopter (should return police cars). + - **67711**: Effective for finding cars when inside a helicopter, not when inside cars. + - **100359**: Similar to flag 70 but often returns the vehicle the player is already in. ``` ## Parameters -* **x**: -* **y**: -* **z**: -* **radius**: -* **modelHash**: -* **flags**: +* **x**: X coordinate of the search location. +* **y**: Y coordinate of the search location. +* **z**: Z coordinate of the search location. +* **radius**: Search radius around the specified coordinates. +* **modelHash**: Hash of a specific vehicle model for the search. Use 0 for any model +* **flags**: Bitwise flags altering search behavior and vehicle inclusion. ## Return value +Closest vehicle that meets the specified criteria. If no matching vehicle is found, returns 0. + +## Examples + +```lua +-- Retrive closest vehicle from the player and print it's id + +-- Retrieve Local Player +local playerPed = PlayerPedId() + +-- Retrieve coordinates of the player +local coordsPlayer = GetEntityCoords(playerPed, false) + +-- Retrieve closest vehicle from the player +local vehicle = GetClosestVehicle(coordsPlayer.x, coordsPlayer.y, coordsPlayer.z, 5.0, 0, 70) + +-- Check if the vehicle exists in the game world. +if not DoesEntityExist(vehicle) then + -- If the vehicle does not exist, end the execution of the code here. + return +end + +-- Print the vehicle id +print(vehicle) +``` + +```js +// Retrive closest vehicle from the player and print it's id + +// Retrieve Local Player +const playerPed = PlayerPedId(); + +// Retrieve coordinates of the player +const [playerX, playerY, playerZ] = GetEntityCoords(playerPed, false); + +// Retrieve closest vehicle from the player +const vehicle = GetClosestVehicle(playerX, playerY, playerZ, 5.0, 0, 70); + +// Check if the vehicle exists in the game world. +if (!DoesEntityExist(vehicle)) { + // If the vehicle does not exist, end the execution of the code here. + return; +} + +// Print the vehicle id +console.log(vehicle); +``` + +```cs +using static CitizenFX.Core.Native.API; + +// Retrive closest vehicle from the player and print it's id + +// Retrieve Local Player +Ped playerPed = PlayerPedId(); + +// Retrieve coordinates of player +Vector3 coordsPlayer = GetEntityCoords(PlayerPedId(), false); + +// Retrieve closest vehicle from player +Vehicle vehicle = GetClosestVehicle(coordsPlayer.X, coordsPlayer.Y, coordsPlayer.Z, 5f, 0, 70); + +// Check if the vehicle exists in the game world. +if (!DoesEntityExist(vehicle)) +{ + // If the vehicle does not exist, end the execution of the code here. + return; +} + +// Print the vehicle id +Debug.WriteLine($"{vehicle}"); +``` \ No newline at end of file