diff --git a/resources/[essential]/es_extended/__resource.lua b/resources/[essential]/es_extended/__resource.lua index 396a1e69..26eb01ef 100644 --- a/resources/[essential]/es_extended/__resource.lua +++ b/resources/[essential]/es_extended/__resource.lua @@ -26,6 +26,7 @@ client_scripts { 'locales/en.lua', 'config.weapons.lua', 'client/common.lua', + 'client/entityiter.lua', 'client/functions.lua', 'client/main.lua' } diff --git a/resources/[essential]/es_extended/client/entityiter.lua b/resources/[essential]/es_extended/client/entityiter.lua new file mode 100644 index 00000000..71e9ee23 --- /dev/null +++ b/resources/[essential]/es_extended/client/entityiter.lua @@ -0,0 +1,79 @@ +--[[The MIT License (MIT) + +Copyright (c) 2017 IllidanS4 + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +]] + +local entityEnumerator = { + __gc = function(enum) + if enum.destructor and enum.handle then + enum.destructor(enum.handle) + end + enum.destructor = nil + enum.handle = nil + end +} + +local function EnumerateEntities(initFunc, moveFunc, disposeFunc) + return coroutine.wrap(function() + local iter, id = initFunc() + if not id or id == 0 then + disposeFunc(iter) + return + end + + local enum = {handle = iter, destructor = disposeFunc} + setmetatable(enum, entityEnumerator) + + local next = true + repeat + coroutine.yield(id) + next, id = moveFunc(iter) + until not next + + enum.destructor, enum.handle = nil, nil + disposeFunc(iter) + end) +end + +function EnumerateObjects() + return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject) +end + +function EnumeratePeds() + return EnumerateEntities(FindFirstPed, FindNextPed, EndFindPed) +end + +function EnumerateVehicles() + return EnumerateEntities(FindFirstVehicle, FindNextVehicle, EndFindVehicle) +end + +function EnumeratePickups() + return EnumerateEntities(FindFirstPickup, FindNextPickup, EndFindPickup) +end + +--[[Usage: +for ped in EnumeratePeds() do + +end +]] \ No newline at end of file diff --git a/resources/[essential]/es_extended/client/functions.lua b/resources/[essential]/es_extended/client/functions.lua index 24b68c95..0ef5872f 100644 --- a/resources/[essential]/es_extended/client/functions.lua +++ b/resources/[essential]/es_extended/client/functions.lua @@ -409,16 +409,11 @@ end ESX.Game.GetObjects = function() - local objects = {} - local handle, object = FindFirstObject() - local success = nil + local objects = {} - repeat + for object in EnumerateObjects() do table.insert(objects, object) - success, object = FindNextObject(handle) - until not success - - EndFindObject(handle) + end return objects @@ -555,16 +550,11 @@ end ESX.Game.GetVehicles = function() - local vehicles = {} - local handle, vehicle = FindFirstVehicle() - local success = nil + local vehicles = {} - repeat + for vehicle in EnumerateVehicles() do table.insert(vehicles, vehicle) - success, vehicle = FindNextVehicle(handle) - until not success - - EndFindVehicle(handle) + end return vehicles @@ -617,6 +607,18 @@ ESX.Game.GetVehiclesInArea = function(coords, area) return vehiclesInArea end +ESX.Game.GetPeds = function() + + local peds = {} + + for ped in EnumeratePeds() do + table.insert(peds, ped) + end + + return peds + +end + ESX.Game.GetVehicleProperties = function(vehicle) local color1, color2 = GetVehicleColours(vehicle)