Events
Script events are named messages with a string payload between the mode and a client script — the server side is Script events.
Receiving
Section titled “Receiving”kcd2mp.on_event(name, fn) |
fn(payload, name) runs for every event of that name; one handler per name, the last registration wins |
kcd2mp.on_any_event = function(name, payload) end |
for every event without a handler of its own |
kcd2mp.on_event("round", function(payload) local n, seconds = payload:match("^(%d+),(%d+)$") System.LogAlways("[arena] round " .. n .. " starts in " .. seconds .. " s")end)
kcd2mp.on_any_event = function(name, payload) System.LogAlways("[arena] unhandled event " .. name .. ": " .. payload)endThe mode sends with SendClientEvent(pid, name, payload) or SendClientEventToAll. A handler
error is logged as [kcd2mp] event 'name' handler: ... and the game goes on.
Sending
Section titled “Sending”kcd2mp.send_event(name, payload) |
to the mode’s OnClientEvent(pid, name, payload) |
The client may send at most 30 events a second and 4 KB each. The payload is any string; the
console command underneath (kcd2mp_event) carries it hex-encoded, so ;, = and quotes are
safe.
kcd2mp.send_event("used", "door 7")kcd2mp.send_event("pos", kcd2mp.player_pos()) -- "x y z" as a string, or "none"Choosing the payload
Section titled “Choosing the payload”A string is the whole contract: a comma list for a handful of numbers, a small JSON document for more. The same convention on both halves is all that matters.
