fivem icon indicating copy to clipboard operation
fivem copied to clipboard

AddStateBagChangeHandler, entity does not exists when event is triggered

Open manzarek123 opened this issue 2 years ago • 3 comments

Hello,

When using the following state bag handler on, a vehicle

AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
    local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
    local vehicle = NetToVeh(netId)
    print(vehicle)
end)

I get the following error: GetNetworkObject: no object by ID 27 0

But the following works fine:

AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
    Citizen.Wait(0)
    local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
    local vehicle = NetToVeh(netId)
    print(vehicle)
end)

I would expect the entity to exist when the handler is triggered.

Repro:

server:

for _, veh in pairs(GetAllVehicles()) do
    Entity(veh).state.bubble = 4
end

client:

AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
    local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
    local vehicle = NetToVeh(netId)
    print(vehicle)
end)

Then leave the onesync zone of the vehicle and enter it again

manzarek123 avatar Feb 18 '22 22:02 manzarek123

Also the Player states are not already updated when the handler is triggered.

Client:

AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
     print(LocalPlayer.state.bubble)
     Wait(0)
    print(LocalPlayer.state.bubble)
end)

Server: Player(1).state.bubble = 4

Results:

nil
4

I would expect the LocalPlayer state to be already updated when the event is triggered

manzarek123 avatar Aug 02 '22 11:08 manzarek123

I would expect the LocalPlayer state to be already updated when the event is triggered

Why? The new value is stored in value after all, so perhaps it would be better to let the old value be accessible at that point.

IS4Code avatar Aug 08 '22 18:08 IS4Code

I would expect the LocalPlayer state to be already updated when the event is triggered

IIRC the design was to offer rejection of statebag changes, although not implemented (needs confirmation), it explains why it's not updated at that point.

thorium-cfx avatar Aug 08 '22 20:08 thorium-cfx