KcdMp.onServer
Registers a function that receives the game mode’s events with a given name.
The game mode’s SendClientEvent(pid, name, payload) - and SendClientEventToAll - reaches every page whose
events_in in ui.toml lets it receive name. fn(value, raw) is called with value, the payload read as JSON when it
is JSON (a table the mode sent arrives as an object or an array), or the text itself when it is not, and raw, the payload
exactly as a text.
The name "*" hears every event the frame may receive; fn then gets the event’s name as a third argument. Events
arrive in the order they were sent, and one sent before the page called KcdMp.ready is kept and delivered when it
does. An event the frame’s events_in does not list never arrives.
Syntax
Section titled “Syntax”KcdMp.onServer(name, fn)| Parameter | Type | |
|---|---|---|
name |
string |
the event’s name (1 to 64 characters of a-z 0-9 _ . : -), or "*" for every event the frame may receive |
fn |
(value: unknown, raw: string, name: string) => void |
called with each event; name is only there for "*" |
Returns
Section titled “Returns”() => void - a function that takes the handler off again
Example
Section titled “Example”// the game mode sent a "shop.items" event whose payload is a table with a list called itemsKcdMp.onServer("shop.items", (value) => { renderItems(value.items);});
// everything the frame may hear, for a logKcdMp.onServer("*", (value, raw, name) => KcdMp.log.info(name + ": " + raw));See also
Section titled “See also”KcdMp.emitServer · KcdMp.onMessage · KcdMp.ready · the Messages and events group of the index
