forked from Simnation/Main
red
This commit is contained in:
parent
9178871ecd
commit
640cdd069b
9 changed files with 583 additions and 269 deletions
|
@ -1,6 +1,25 @@
|
|||
EntitiesStates = {}
|
||||
|
||||
local function IsEntityStateLoaded(uNetId)
|
||||
return EntitiesStates[uNetId] ~= -1
|
||||
end
|
||||
|
||||
local function EnsureStateLoaded(uNetId)
|
||||
if not IsEntityStateLoaded(uNetId) then
|
||||
local start = GetGameTimer()
|
||||
while not IsEntityStateLoaded(uNetId) do
|
||||
if GetGameTimer() - start > 5000 then
|
||||
error("WaitUntilStateLoaded: entity "..tostring(uNetId).." state loading timed out")
|
||||
break
|
||||
end
|
||||
Citizen.Wait(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RegisterNetEvent("Utility:Net:UpdateStateValue", function(uNetId, key, value)
|
||||
EnsureStateLoaded(uNetId)
|
||||
|
||||
if not EntitiesStates[uNetId] then
|
||||
EntitiesStates[uNetId] = {}
|
||||
end
|
||||
|
@ -9,14 +28,49 @@ RegisterNetEvent("Utility:Net:UpdateStateValue", function(uNetId, key, value)
|
|||
end)
|
||||
|
||||
GetEntityStateValue = function(uNetId, key)
|
||||
if not EntitiesStates[uNetId] then
|
||||
return
|
||||
if not UtilityNet.GetEntityFromUNetId(uNetId) then -- If trying to get state of entity that isnt loaded
|
||||
local start = GetGameTimer()
|
||||
|
||||
local entity = nil
|
||||
while not entity do
|
||||
entity = UtilityNet.InternalFindFromNetId(uNetId)
|
||||
if GetGameTimer() - start > 2000 then
|
||||
error("GetEntityStateValue: entity "..tostring(uNetId).." doesnt exist, attempted to retrieve key: "..tostring(key))
|
||||
break
|
||||
end
|
||||
Citizen.Wait(1)
|
||||
end
|
||||
|
||||
return ServerRequestEntityKey(uNetId, key)
|
||||
else
|
||||
EnsureStateLoaded(uNetId)
|
||||
|
||||
if not EntitiesStates[uNetId] then
|
||||
warn("GetEntityStateValue: entity "..tostring(uNetId).." has no loaded states, attempted to retrieve key: "..tostring(key))
|
||||
return
|
||||
end
|
||||
|
||||
return EntitiesStates[uNetId][key]
|
||||
end
|
||||
|
||||
return EntitiesStates[uNetId][key]
|
||||
end
|
||||
|
||||
ServerRequestEntityKey = function(uNetId, key)
|
||||
local p = promise:new()
|
||||
local event = nil
|
||||
|
||||
event = RegisterNetEvent("Utility:Net:GetStateValue"..uNetId, function(value)
|
||||
RemoveEventHandler(event)
|
||||
p:resolve(value)
|
||||
end)
|
||||
|
||||
TriggerServerEvent("Utility:Net:GetStateValue", uNetId, key)
|
||||
return Citizen.Await(p)
|
||||
end
|
||||
|
||||
ServerRequestEntityStates = function(uNetId)
|
||||
EntitiesStates[uNetId] = -1 -- Set as loading
|
||||
|
||||
local p = promise:new()
|
||||
local event = nil
|
||||
|
||||
|
@ -28,7 +82,7 @@ ServerRequestEntityStates = function(uNetId)
|
|||
TriggerServerEvent("Utility:Net:GetState", uNetId)
|
||||
local states = Citizen.Await(p)
|
||||
|
||||
EntitiesStates[uNetId] = states
|
||||
EntitiesStates[uNetId] = states or {}
|
||||
end
|
||||
|
||||
exports("GetEntityStateValue", GetEntityStateValue)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue