From ac5e7256de4db51c37950284485072d8ddb469b3 Mon Sep 17 00:00:00 2001 From: Linden Date: Sun, 4 Jul 2021 05:01:09 +1000 Subject: [PATCH 1/2] improvement(esx): Replace entity iterator with GetGamePool natives --- [esx]/es_extended/client/functions.lua | 58 +++++++++++++++----------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/[esx]/es_extended/client/functions.lua b/[esx]/es_extended/client/functions.lua index 09174c08..a4c2248d 100644 --- a/[esx]/es_extended/client/functions.lua +++ b/[esx]/es_extended/client/functions.lua @@ -402,7 +402,6 @@ ESX.Game.SpawnVehicle = function(vehicle, coords, heading, cb, networked) end ESX.Game.SpawnLocalVehicle = function(vehicle, coords, heading, cb) - -- Why have 2 separate functions for this? Just call the other one with an extra param ESX.Game.SpawnVehicle(vehicle, coords, heading, cb, false) end @@ -413,20 +412,14 @@ ESX.Game.IsVehicleEmpty = function(vehicle) return passengers == 0 and driverSeatFree end -ESX.Game.GetObjects = function() - local objects = {} - - for object in EnumerateObjects() do - table.insert(objects, object) - end - - return objects +ESX.Game.GetObjects = function() -- Leave the function for compatibility + return GetGamePool('CObject') end ESX.Game.GetPeds = function(onlyOtherPeds) local peds, myPed = {}, ESX.PlayerData.ped - for ped in EnumeratePeds() do + for ped in GetGamePool('CPed') do if ((onlyOtherPeds and ped ~= myPed) or not onlyOtherPeds) then table.insert(peds, ped) end @@ -435,14 +428,8 @@ ESX.Game.GetPeds = function(onlyOtherPeds) return peds end -ESX.Game.GetVehicles = function() - local vehicles = {} - - for vehicle in EnumerateVehicles() do - table.insert(vehicles, vehicle) - end - - return vehicles +ESX.Game.GetVehicles = function() -- Leave the function for compatibility + return GetGamePool('CVehicle') end ESX.Game.GetPlayers = function(onlyOtherPlayers, returnKeyValue, returnPeds) @@ -463,13 +450,34 @@ ESX.Game.GetPlayers = function(onlyOtherPlayers, returnKeyValue, returnPeds) return players end -ESX.Game.GetClosestObject = function(coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetObjects(), false, coords, modelFilter) end -ESX.Game.GetClosestPed = function(coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetPeds(true), false, coords, modelFilter) end -ESX.Game.GetClosestPlayer = function(coords) return ESX.Game.GetClosestEntity(ESX.Game.GetPlayers(true, true), true, coords, nil) end -ESX.Game.GetClosestVehicle = function(coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetVehicles(), false, coords, modelFilter) end -ESX.Game.GetPlayersInArea = function(coords, maxDistance) return EnumerateEntitiesWithinDistance(ESX.Game.GetPlayers(true, true), true, coords, maxDistance) end -ESX.Game.GetVehiclesInArea = function(coords, maxDistance) return EnumerateEntitiesWithinDistance(ESX.Game.GetVehicles(), false, coords, maxDistance) end -ESX.Game.IsSpawnPointClear = function(coords, maxDistance) return #ESX.Game.GetVehiclesInArea(coords, maxDistance) == 0 end +ESX.Game.GetClosestObject = function(coords, modelFilter) + return ESX.Game.GetClosestEntity(ESX.Game.GetObjects(), false, coords, modelFilter) +end + +ESX.Game.GetClosestPed = function(coords, modelFilter) + return ESX.Game.GetClosestEntity(ESX.Game.GetPeds(true), false, coords, modelFilter) +end + +ESX.Game.GetClosestPlayer = function(coords) + return ESX.Game.GetClosestEntity(ESX.Game.GetPlayers(true, true), true, coords, nil) +end + +ESX.Game.GetClosestVehicle = function(coords, modelFilter) + return ESX.Game.GetClosestEntity(ESX.Game.GetVehicles(), false, coords, modelFilter) +end + +ESX.Game.GetPlayersInArea = function(coords, maxDistance) + return EnumerateEntitiesWithinDistance(ESX.Game.GetPlayers(true, true), true, coords, maxDistance) +end + +ESX.Game.GetVehiclesInArea = function(coords, maxDistance) + return EnumerateEntitiesWithinDistance(ESX.Game.GetVehicles(), false, coords, maxDistance) +end + +ESX.Game.IsSpawnPointClear = function(coords, maxDistance) + return #ESX.Game.GetVehiclesInArea(coords, maxDistance) == 0 +end + ESX.Game.GetClosestEntity = function(entities, isPlayerEntities, coords, modelFilter) local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil From e286bc86028222c19947a56c2202374db615b2f6 Mon Sep 17 00:00:00 2001 From: Linden Date: Mon, 5 Jul 2021 18:02:45 +1000 Subject: [PATCH 2/2] fix(society): Correct getOnlinePlayers behaviour Actually prevent multiple loops from running at once --- [esx_addons]/esx_society/server/main.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/[esx_addons]/esx_society/server/main.lua b/[esx_addons]/esx_society/server/main.lua index 08e0dc0e..0cab1a49 100644 --- a/[esx_addons]/esx_society/server/main.lua +++ b/[esx_addons]/esx_society/server/main.lua @@ -316,9 +316,9 @@ ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, end end) -local getOnlinePlayers, onlinePlayers = false, nil +local getOnlinePlayers, onlinePlayers = false, {} ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb) - if getOnlinePlayers ~= true then -- Prevent multiple xPlayer loops from running in quick succession + if getOnlinePlayers == false and next(onlinePlayers) == nil then -- Prevent multiple xPlayer loops from running in quick succession getOnlinePlayers, onlinePlayers = true, {} local xPlayers = ESX.GetExtendedPlayers() @@ -330,9 +330,13 @@ ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb) job = xPlayer.job }) end + cb(onlinePlayers) getOnlinePlayers = false + Citizen.Wait(1000) -- For the next second any extra requests will receive the cached list + onlinePlayers = {} + return end - while getOnlinePlayers do Citizen.Wait(100) end -- Wait for the xPlayer loop to finish + while getOnlinePlayers do Citizen.Wait(10) end -- Wait for the xPlayer loop to finish cb(onlinePlayers) end)