points update

to allow args to be passed and modified
This commit is contained in:
gononono64 committed 2025-03-26 16:12:38 -05:00
1 parent 9ee811c2d0
commit 0b67e4b029
1 file changed
+17 -18
+17 -18
View File
@@ -3,6 +3,8 @@ Point = {}
local ActivePoints = {} local ActivePoints = {}
local GridCells = {} local GridCells = {}
local LoopStarted = false local LoopStarted = false
local insidePoints = {}
local data = {} -- 👈 Move this outside the function
-- Grid configuration -- Grid configuration
local GRID_SIZE = 500.0 -- Size of each grid cell local GRID_SIZE = 500.0 -- Size of each grid cell
@@ -97,12 +99,11 @@ function Point.CheckPointsInSameCell(point)
return nearbyPoints return nearbyPoints
end end
local insidePoints = {}
---Internal function that starts the loop. Do not call this function directly. ---Internal function that starts the loop. Do not call this function directly.
function Point.StartLoop() function Point.StartLoop()
if LoopStarted then return false end if LoopStarted then return false end
LoopStarted = true LoopStarted = true
-- Remove the "local data = {}" line from here
CreateThread(function() CreateThread(function()
while LoopStarted do while LoopStarted do
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
@@ -138,15 +139,18 @@ function Point.StartLoop()
-- Check if player entered/exited the point -- Check if player entered/exited the point
if distance < point.distance then if distance < point.distance then
if not point.inside then if not point.inside then
print("Entered point: ", point.distance)
point.inside = true point.inside = true
point.onEnter(point) data[point.id] = data[point.id] or point.args or {}
data[point.id] = point.onEnter(point, data[point.id])
insidePoints[point.id] = point insidePoints[point.id] = point
end end
-- Modified main loop exit handler
elseif point.inside then elseif point.inside then
print("Exited point: ", point.id)
point.inside = false point.inside = false
point.onExit(point) data[point.id] = data[point.id] or point.args or {}
local result = point.onExit(point, data[point.id])
data[point.id] = result ~= nil and result or data[point.id] -- ← Use consistent fallback
point.args = data[point.id] -- ← Update point.args to match data[point.id]
insidePoints[point.id] = nil insidePoints[point.id] = nil
end end
@@ -161,20 +165,14 @@ function Point.StartLoop()
local pos = insidepoint.coords and vector3(insidepoint.coords.x, insidepoint.coords.y, insidepoint.coords.z) or vector3(0, 0, 0) local pos = insidepoint.coords and vector3(insidepoint.coords.x, insidepoint.coords.y, insidepoint.coords.z) or vector3(0, 0, 0)
local dist = #(playerCoords - pos) local dist = #(playerCoords - pos)
if dist > insidepoint.distance then if dist > insidepoint.distance then
print("Exited point: ", insidepoint.id)
insidepoint.inside = false insidepoint.inside = false
insidepoint.onExit(insidepoint) data[insidepoint.id] = data[insidepoint.id] or insidepoint.args or {}
local result = insidepoint.onExit(insidepoint, data[insidepoint.id])
data[insidepoint.id] = result ~= nil and result or data[insidepoint.id]
insidepoint.args = data[insidepoint.id] -- ← Keep data in sync
insidePoints[insidepoint.id] = nil insidePoints[insidepoint.id] = nil
end end
end end
--kills the loop if no targets exist
--[[
if not targetsExist then
LoopStarted = false
break
end
--]]
Wait(waitTime) -- Faster updates when moving quickly Wait(waitTime) -- Faster updates when moving quickly
end end
end) end)
@@ -190,11 +188,11 @@ end
---@param _onNearby function ---@param _onNearby function
---@param data self ---@param data self
---@return table ---@return table
function Point.Register(id, target, distance, _onEnter, _onExit, _onNearby, data) function Point.Register(id, target, distance, args, _onEnter, _onExit, _onNearby)
local isEntity = type(target) == "number" local isEntity = type(target) == "number"
local coords = isEntity and GetEntityCoords(target) or target local coords = isEntity and GetEntityCoords(target) or target
local self = data or {} local self = {}
self.id = id self.id = id
self.target = target -- Store entity ID or Vector3 self.target = target -- Store entity ID or Vector3
self.isEntity = isEntity self.isEntity = isEntity
@@ -204,6 +202,7 @@ function Point.Register(id, target, distance, _onEnter, _onExit, _onNearby, data
self.onExit = _onExit or function() end self.onExit = _onExit or function() end
self.onNearby = _onNearby or function() end self.onNearby = _onNearby or function() end
self.inside = false -- Track if player is inside self.inside = false -- Track if player is inside
self.args = args or {}
ActivePoints[id] = self ActivePoints[id] = self
Point.RegisterInGrid(self) Point.RegisterInGrid(self)