Cache and entities update
added cache system similar to others that are readily available. Included beginings of reworked rebound entities
This commit is contained in:
1 parent
b9842eedfe
commit
89858c6d73
16 files changed
+589
-207
No files matched your search
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
Cache = Cache or Require("lib/cache/shared/cache.lua")
|
||||
|
||||
-- Comparison functions for client-side state
|
||||
local function GetPed()
|
||||
return PlayerPedId()
|
||||
end
|
||||
|
||||
local function GetVehicle()
|
||||
local ped = Cache.Get("Ped") -- Use cached ped for efficiency
|
||||
if not ped then return end -- Or handle error/default case
|
||||
return GetVehiclePedIsIn(ped, false)
|
||||
end
|
||||
|
||||
local function GetVehicleSeat()
|
||||
local vehicle = Cache.Get("Vehicle")
|
||||
if not vehicle then return end
|
||||
local ped = Cache.Get("Ped")
|
||||
if not ped then return end
|
||||
for i = -1, GetVehicleMaxNumberOfPassengers(vehicle) - 1 do
|
||||
if ped == GetPedInVehicleSeat(vehicle, i) then
|
||||
return i
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function GetWeapon()
|
||||
local ped = Cache.Get("Ped")
|
||||
if not ped then return end -- Return unarmed hash if no ped
|
||||
return GetSelectedPedWeapon(ped)
|
||||
end
|
||||
|
||||
AddEventHandler('onResourceStart', function(resource)
|
||||
if resource ~= GetCurrentResourceName() then return end
|
||||
Cache.Create("Ped", GetPed, 1000) -- Check ped every second (adjust as needed)
|
||||
Cache.Create("Vehicle", GetVehicle, 500) -- Check vehicle every 500ms
|
||||
Cache.Create("Seat", GetVehicleSeat, 500) -- Check if in vehicle every 500ms
|
||||
Cache.Create("Weapon", GetWeapon, 250) -- Check weapon every 250ms
|
||||
end)
|
||||
|
||||
return Cache
|
||||
Vendored
Whitespace-only changes.
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
Cache = {}
|
||||
Cache.Caches = {}
|
||||
|
||||
Cache.LoopRunning = false
|
||||
|
||||
local max = 5000
|
||||
local function StartLoop()
|
||||
if Cache.LoopRunning then return end
|
||||
Cache.LoopRunning = true
|
||||
CreateThread(function()
|
||||
while Cache.LoopRunning do
|
||||
local now = GetGameTimer()
|
||||
local minWait = nil
|
||||
local caches = Cache.Caches
|
||||
for name, cache in pairs(caches) do
|
||||
if cache.Compare then
|
||||
cache.LastChecked = cache.LastChecked or now
|
||||
cache.WaitTime = cache.WaitTime or max
|
||||
local elapsed = now - cache.LastChecked
|
||||
local remaining = cache.WaitTime - elapsed
|
||||
if remaining <= 0 then
|
||||
local oldValue = cache.Value
|
||||
cache.Value = cache.Compare()
|
||||
if cache.Value ~= oldValue and cache.OnChange then
|
||||
for _, onChange in pairs(cache.OnChange) do
|
||||
onChange(cache.Value, oldValue)
|
||||
end
|
||||
end
|
||||
cache.LastChecked = now
|
||||
remaining = cache.WaitTime
|
||||
end
|
||||
if not minWait or remaining < minWait then
|
||||
minWait = remaining
|
||||
if minWait <= 0 then break end
|
||||
end
|
||||
end
|
||||
end
|
||||
if minWait then
|
||||
Wait(math.max(0, minWait))
|
||||
else
|
||||
Wait(max)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Cache.Create(name, compare, waitTime)
|
||||
Cache.Caches[name] = Cache.Caches[name] or {}
|
||||
Cache.Caches[name].WaitTime = waitTime or max
|
||||
Cache.Caches[name].Compare = compare
|
||||
Cache.Caches[name].OnChange = Cache.Caches[name].OnChange or {}
|
||||
Cache.Caches[name].Value = compare()
|
||||
print("Cache created: " .. name .. " with initial value: " .. tostring(Cache.Caches[name].Value))
|
||||
for _, onChange in pairs(Cache.Caches[name].OnChange) do
|
||||
onChange(Cache.Caches[name].Value, nil)
|
||||
end
|
||||
return StartLoop()
|
||||
end
|
||||
|
||||
function Cache.Get(name)
|
||||
local cache = Cache.Caches[name]
|
||||
return cache and cache.Value or nil
|
||||
end
|
||||
|
||||
function Cache.OnChange(name, onChange)
|
||||
local cache = Cache.Caches[name]
|
||||
assert(cache, "Cache with name '" .. name .. "' does not exist.")
|
||||
table.insert(cache.OnChange, onChange)
|
||||
end
|
||||
|
||||
return Cache
|
||||
|
||||
|
||||
Reference in new issue
Block a user