From 37032fe0dd2fe3a988b77087412ecfa54adcea2e Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Mon, 28 Feb 2022 00:49:44 +0000 Subject: [PATCH] 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. --- resource/main.lua | 4 +-- resource/states/client.lua | 63 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 resource/states/client.lua diff --git a/resource/main.lua b/resource/main.lua index 5342930..4872acc 100644 --- a/resource/main.lua +++ b/resource/main.lua @@ -1,10 +1,10 @@ -- Creates exports from values assigned to lib -- Hacky workaround for Lua Language Server support ---- Alias for `exports['pe-lualib']` +--- Alias for `exports.ox_lib` lib = setmetatable({}, { __newindex = function(self, name, fn) exports(name, fn) rawset(self, name, fn) end -}) \ No newline at end of file +}) diff --git a/resource/states/client.lua b/resource/states/client.lua new file mode 100644 index 0000000..8f96407 --- /dev/null +++ b/resource/states/client.lua @@ -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)