Merge pull request #1554 from feelfreetofee/dev

refactor(es_extended/client/imports/point.lua) and (es_extended/client/modules/points.lua): Performace Improvement
This commit is contained in:
Kasey Fitton
2024-12-29 00:16:45 +00:00
committed by GitHub
2 changed files with 106 additions and 100 deletions
+21 -12
View File
@@ -1,31 +1,40 @@
Point = ESX.Class() local Point = ESX.Class()
local nearby, loop = {}
function Point:constructor(properties) function Point:constructor(properties)
self.coords = properties.coords self.coords = properties.coords
self.hidden = properties.hidden or false self.hidden = properties.hidden
self.inside = properties.inside or function() end self.enter = properties.enter
self.enter = properties.enter or function() end self.leave = properties.leave
self.leave = properties.leave or function() end self.inside = properties.inside
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function() self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
self.nearby = true nearby[self.handle] = self
if self.enter then if self.enter then
self:enter() self:enter()
end end
if self.inside then if not loop then
loop = true
CreateThread(function() CreateThread(function()
while self.nearby do while loop do
local coords = GetEntityCoords(ESX.PlayerData.ped) local coords = GetEntityCoords(ESX.PlayerData.ped)
self.currDistance = #(coords - self.coords) for handle, point in pairs(nearby) do
self:inside() if point.inside then
Wait(0) point:inside(#(coords - point.coords))
end
end
Wait()
end end
end) end)
end end
end, function() end, function()
self.nearby = false nearby[self.handle] = nil
if self.leave then if self.leave then
self:leave() self:leave()
end end
if #nearby == 0 then
loop = false
end
end) end)
end end
+22 -25
View File
@@ -25,33 +25,30 @@ function ESX.HidePointInternal(handle, hidden)
end end
function StartPointsLoop() function StartPointsLoop()
CreateThread(function() CreateThread(function()
while true do while true do
local coords = GetEntityCoords(ESX.PlayerData.ped) local coords = GetEntityCoords(ESX.PlayerData.ped)
for i, point in pairs(points) do for handle, point in pairs(points) do
local distance = #(coords - point.coords) if not point.hidden and #(coords - point.coords) <= point.distance then
if not point.nearby then
if not point.hidden and distance <= point.distance then points[handle].nearby = true
if not point.nearby then points[handle].enter()
points[i].nearby = true end
points[i].enter() elseif point.nearby then
end points[handle].nearby = false
point.currentDistance = distance points[handle].leave()
elseif point.nearby then end
points[i].nearby = false end
points[i].leave() Wait(500)
end end
end end)
Wait(500)
end
end)
end end
AddEventHandler('onResourceStop', function(resource) AddEventHandler('onResourceStop', function(resource)
for i, point in pairs(points) do for handle, point in pairs(points) do
if point.resource == resource then if point.resource == resource then
points[i] = nil points[handle] = nil
end end
end end
end) end)