perf(zones): assign inside count and index to local

Reduce number of times the inside table needs to be indexed or have length checked.
Also assign debug zones to table without having to be inside the zone.
This commit is contained in:
Linden
2022-04-27 17:07:24 +10:00
parent cc770ada64
commit a340c83e1a

View File

@@ -21,20 +21,23 @@ local function debug(self)
end end
local inside = {} local inside = {}
local insideCount
local tick local tick
CreateThread(function() CreateThread(function()
while true do while true do
local coords = GetEntityCoords(cache.ped)
Wait(300) Wait(300)
local coords = GetEntityCoords(cache.ped)
table.wipe(inside) table.wipe(inside)
insideCount = 0
for _, zone in pairs(zones) do for _, zone in pairs(zones) do
local contains = zone.polygon:contains(coords, zone.thickness) local contains = zone.polygon:contains(coords, zone.thickness)
if contains then if contains then
if zone.inside then if zone.inside then
inside[#inside + 1] = zone insideCount += 1
inside[insideCount] = zone
end end
if zone.onEnter and not zone.insideZone then if zone.onEnter and not zone.insideZone then
@@ -46,21 +49,29 @@ CreateThread(function()
if zone.onExit then if zone.onExit then
zone:onExit() zone:onExit()
end end
elseif zone.debug then
insideCount += 1
inside[insideCount] = zone
end end
end end
if not tick then if not tick then
if #inside > 0 then if insideCount > 0 then
tick = SetInterval(function() tick = SetInterval(function()
for i = 1, #inside do for i = 1, insideCount do
inside[i]:inside() local zone = inside[i]
if inside[i].debug then
inside[i]:debug() if zone.insideZone then
zone:inside()
end
if zone.debug then
zone:debug()
end end
end end
end) end)
end end
elseif #inside == 0 then elseif insideCount == 0 then
tick = ClearInterval(tick) tick = ClearInterval(tick)
end end
end end