Skip to content

Instantly share code, notes, and snippets.

@ctsmax
Created May 5, 2021 12:40
Show Gist options
  • Save ctsmax/8689f548667435300f3821336a2238f4 to your computer and use it in GitHub Desktop.
Save ctsmax/8689f548667435300f3821336a2238f4 to your computer and use it in GitHub Desktop.
FiveM entity enumerator made in js
const iterator = (first, move, dispose) =>
{
const ret = [];
const [handle, firstEntity] = first();
if (!handle) {
return ret;
}
ret.push(firstEntity);
let next = true;
while (next)
{
const [valid, entity] = move(handle);
if (valid) {
ret.push(entity);
}
next = valid;
}
dispose(handle);
return ret;
}
const EnumerateObjects = () => iterator(FindFirstObject, FindNextObject, EndFindObject);
const EnumeratePeds = () => iterator(FindFirstPed, FindNextPed, EndFindPed);
const EnumerateVehicles = () => iterator(FindFirstVehicle, FindNextVehicle, EndFindVehicle);
const EnumeratePickups = () => iterator(FindFirstPickup, FindNextPickup, EndFindPickup);
// example on how to use the enumerator
const GetClosestVehicle = () =>
{
let closestEntityDistance = -1;
const localPlayer = GetPlayerPed(-1);
const coords = GetEntityCoords(localPlayer);
EnumerateVehicles().forEach((entity) => {
const entityCoords = GetEntityCoords(entity);
const distance = GetDistanceBetweenCoords(coords[0], coords[1], coords[2], entityCoords[0], entityCoords[1], entityCoords[2], true);
if (closestEntityDistance === -1 || distance < closestEntityDistance){
closestEntityDistance = distance
}
});
return closestEntityDistance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment