Skip to content

Commit

Permalink
refactor(client/imports/points.lua): Performance improvement
Browse files Browse the repository at this point in the history
The thread that handles the inside property is now created in every resource as long the player is nearby a point create by the same resource.
  • Loading branch information
feelfreetofee authored Dec 19, 2024
1 parent f7eb0ca commit f1240a1
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions [core]/es_extended/client/imports/point.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
Point = ESX.Class()
local Point = ESX.Class()

local nearby, loop = {}

function Point:constructor(properties)
self.coords = properties.coords
self.hidden = properties.hidden or false
self.inside = properties.inside or function() end
self.enter = properties.enter or function() end
self.leave = properties.leave or function() end
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
self.nearby = true
if self.enter then
self:enter()
end
if self.inside then
CreateThread(function()
while self.nearby do
local coords = GetEntityCoords(ESX.PlayerData.ped)
self.currDistance = #(coords - self.coords)
self:inside()
Wait(0)
end
end)
end
end, function()
self.nearby = false
if self.leave then
self:leave()
end
end)
self.coords = properties.coords
self.hidden = properties.hidden
self.enter = properties.enter
self.leave = properties.leave
self.inside = properties.inside
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
nearby[self.handle] = self
if self.enter then
self:enter()
end
if not loop then
loop = true
CreateThread(function()
while loop do
local coords = GetEntityCoords(ESX.PlayerData.ped)
for handle, point in pairs(nearby) do
if point.inside then
point:inside(#(coords - point.coords))
end
end
Wait()
end
end)
end
end, function()
nearby[self.handle] = nil
if self.leave then
self:leave()
end
if #nearby == 0 then
loop = false
end
end)
end

function Point:delete()
ESX.RemovePointInternal(self.handle)
ESX.RemovePointInternal(self.handle)
end

function Point:toggle(hidden)
if hidden == nil then
hidden = not self.hidden
end
self.hidden = hidden
ESX.HidePointInternal(self.handle, hidden)
if hidden == nil then
hidden = not self.hidden
end
self.hidden = hidden
ESX.HidePointInternal(self.handle, hidden)
end

return Point
return Point

0 comments on commit f1240a1

Please sign in to comment.