Skip to content

State

Whatever the mode puts in a state bagSetGlobalState, SetPlayerState, SetEntityState — arrives on every client as strings in kcd2mp.state.

kcd2mp.state.global[key] the world’s bag
kcd2mp.state.player[pid][key] every player’s, from their join on; this client’s own is kcd2mp.state.player[kcd2mp.player_id]
kcd2mp.state.entity[netId][key] while the entity is in view: the bag arrives with its spawn and is dropped with its despawn

Values are strings (the server converts numbers and booleans); a removed key reads nil. The global and the players’ bags are delivered whole at the join, before this client’s own spawn, so a script may read them the moment it runs.

local round = tonumber(kcd2mp.state.global.round) or 0
local myTeam = (kcd2mp.state.player[kcd2mp.player_id] or {}).team
kcd2mp.on_state(key, fn) fn(value, scope, id) for one key, in any scope (scope is "global", "player" or "entity"; id the pid or net id, 0 for global)
kcd2mp.on_state_change = function(scope, id, key, value) end for every change; value nil = removed
kcd2mp.on_state("team", function(value, scope, pid)
if pid == kcd2mp.player_id then
System.LogAlways("[arena] you are on team " .. tostring(value))
end
end)
kcd2mp.on_state_change = function(scope, id, key, value)
System.LogAlways(string.format("[arena] state %s %s %s = %s", scope, tostring(id), key, tostring(value)))
end

Both hooks run inside the game’s Lua on the game thread; an error in one is logged ([kcd2mp] state 'key' handler: ...) and does not stop the update.

A hot reload of the mode tears every bag down on the server; the new mode sets them again from OnGameModeInit on.