mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(states): WIP handler for cacheing and setting states
Provide a centralised non-framework dependent interval to track and perform tasks based on certain variables, such as if the player is driving, in a vehicle, or walking. Will later add a statebag change handler with callbacks, to easily listen for state changes and use them in third-party resources.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
-- Creates exports from values assigned to lib
|
-- Creates exports from values assigned to lib
|
||||||
-- Hacky workaround for Lua Language Server support
|
-- Hacky workaround for Lua Language Server support
|
||||||
--- Alias for `exports['pe-lualib']`
|
--- Alias for `exports.ox_lib`
|
||||||
|
|
||||||
lib = setmetatable({}, {
|
lib = setmetatable({}, {
|
||||||
__newindex = function(self, name, fn)
|
__newindex = function(self, name, fn)
|
||||||
|
|||||||
63
resource/states/client.lua
Normal file
63
resource/states/client.lua
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
local cache = {}
|
||||||
|
local player = LocalPlayer.state
|
||||||
|
local states = {
|
||||||
|
['vehicle'] = true,
|
||||||
|
['driver'] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function cache:set(key, value)
|
||||||
|
if value ~= self[key] then
|
||||||
|
self[key] = value
|
||||||
|
|
||||||
|
if states[key] then player:set(key, value, false) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:getPed()
|
||||||
|
local ped = PlayerPedId()
|
||||||
|
if ped ~= self.ped then
|
||||||
|
self.ped = ped
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:getVehicle()
|
||||||
|
local vehicle = GetVehiclePedIsIn(self.ped, false)
|
||||||
|
if vehicle > 0 then
|
||||||
|
if vehicle ~= self.vehicle then
|
||||||
|
self:set('vehicle', vehicle)
|
||||||
|
|
||||||
|
if GetPedInVehicleSeat(vehicle, -1) == self.ped then
|
||||||
|
self:set('driver', true)
|
||||||
|
else
|
||||||
|
self:set('driver', false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else self:set('vehicle', nil) end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:onFoot()
|
||||||
|
-- print('on foot')
|
||||||
|
end
|
||||||
|
|
||||||
|
function cache:inVehicle()
|
||||||
|
if self.driver then
|
||||||
|
-- print('driving vehicle')
|
||||||
|
else
|
||||||
|
-- print('in vehicle')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
CreateThread(function()
|
||||||
|
while true do
|
||||||
|
cache:getPed()
|
||||||
|
cache:getVehicle()
|
||||||
|
|
||||||
|
if not cache.vehicle then
|
||||||
|
cache:onFoot()
|
||||||
|
else
|
||||||
|
cache:inVehicle()
|
||||||
|
end
|
||||||
|
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
end)
|
||||||
Reference in New Issue
Block a user