Script events
A game mode that needs something on the client — a marker, a sound, a bit of UI, a value read from
the game — ships a client script and talks to it through named events with a string payload,
in both directions. The client half is the game’s own Lua plus the kcd2mp table.
Server side
Section titled “Server side”| Function | |
|---|---|
SendClientEvent(pid, name, payload) |
to one player’s client script; pid nil = everyone |
SendClientEventToAll(name, payload) |
to everyone’s |
function OnClientEvent(pid, name, payload) end -- from a client: at most 30 a second and 4 KB eachBoth halves
Section titled “Both halves”SendClientEvent(pid, "marker", "1280.5,1088.0,3") -- to one client; SendClientEventToAll for everyone
function OnClientEvent(pid, name, payload) if name == "used" then Log(GetPlayerName(pid) .. " used " .. payload) endendkcd2mp.on_event("marker", function(payload) -- one handler per name ... local x, y, z = payload:match("^([^,]+),([^,]+),(.+)$") System.LogAlways("[kcd2mp] marker at " .. x .. " " .. y)end)
kcd2mp.on_any_event = function(name, payload) end -- ... or a catch-all for the rest
kcd2mp.send_event("used", "door 7") -- to the mode's OnClientEventThe payload is a string: JSON, a comma list, whatever the two halves agree on. A handler error on
the client is logged ([kcd2mp] event 'name' handler: ...), never fatal.
When a state bag is the better tool
Section titled “When a state bag is the better tool”An event is a message; it reaches the clients that are connected when it is sent and is gone. A
value every client should be able to read at any time — a team, a round number, who carries the
flag — belongs in a state bag: set once on the server, replicated to every
client’s kcd2mp.state, delivered whole to newcomers.
