add Client State Bag

This commit is contained in:
JericoFX committed 2025-05-05 19:24:17 -03:00
1 parent aa1e621f90
commit f913089d36
3 files changed
+50 -2

No files matched your search

+2 -2
View File
@@ -73,5 +73,5 @@ cLib.Particle = Particle or Require("lib/particles/client/particles.lua")
cLib.Cache = Cache or Require("lib/cache/client/cache.lua")
cLib.ClientEntity = ClientEntity or Require("lib/entities/client/client_entity.lua")
cLib.ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua")
return cLib
cLib.ClientStateBag = ClientStateBag or Require("lib/statebags/client/client.lua")
return cLib
+48
View File
@@ -0,0 +1,48 @@
ClientStateBag = {}
---Gets an entity from a statebag name
---@param entityId string The entity ID or statebag name
---@return number|nil entity The entity handle or nil if not found
local function getStateBagEntity(entityId)
local _entity = GetEntityFromStateBagName(entityId)
return _entity
end
local function getPlayerFromStateBagName(stateBagName)
return getPlayerFromStateBagName(stateBagName)
end
---Adds a handler for entity statebag changes
---@param keyName string The statebag key to watch for changes
---@param entityId string|nil The specific entity ID to watch, or nil for all entities
---@param callback function The callback function to handle changes (function(entityId, key, value, lastValue, replicated))
---@return number handler The handler ID
function ClientStateBag.AddEntityChangeHandler(keyName, entityId, callback)
return AddStateBagChangeHandler(keyName, entityId or nil, function(bagName, key, value, lastValue, replicated)
local entity = getStateBagEntity(bagName)
if not DoesEntityExist(entity) then return end
if entity then
return callback(entity, key, value, lastValue, replicated)
end
end)
end
---Adds a handler for player statebag changes
---@param keyName string The statebag key to watch for changes
---@param filter boolean|nil If true, only watch for changes from the current player
---@param callback function The callback function to handle changes (function(playerId, key, value, lastValue, replicated))
---@return number handler The handler ID
function ClientStateBag.AddPlayerChangeHandler(keyName, filter, callback)
return AddStateBagChangeHandler(keyName, filter and GetPlayerServerId(PlayerId()) or nil,
function(bagName, key, value, lastValue, replicated)
local actualPlayerId = getPlayerFromStateBagName(bagName)
if DoesEntityExist(actualPlayerId) and (not actualPlayerId == PlayerPedId()) then -- you cant have a statebag value if you are not the player
return false
end
if actualPlayerId and actualPlayerId ~= 0 then
return callback(tonumber(actualPlayerId), key, value, lastValue, replicated)
end
end)
end
return ClientStateBag
View File
Whitespace-only changes.