Skip to content

KcdMp.emitServer

Sends the game mode an event.

The event arrives in the mode’s OnClientEvent(pid, name, payload, source) with the frame’s name as source. value is sent as it is when it is a text and as JSON when it is anything else (nothing sends an empty payload); it is at most 16 KB. The promise resolves once the game has sent the event - not when the mode has handled it.

It rejects with an Error when the event cannot be sent: "that event is not in the frame's events_out" (the name is not one the frame may send), "too many events" (the player is over their budget of 30 events and 64 KB a second, shared with their client scripts), "the payload is over 16 KB", "not connected to a server".

The mode treats what a page sends like any other input from a player: source says which frame sent it, and nothing more.

await KcdMp.emitServer(name, value?)
Parameter Type
name string the event’s name, 1 to 64 characters of a-z 0-9 _ . : -; not one that starts with kcdmp:
value unknown the payload - a text as it is, anything else as JSON (optional)

Promise<void> - settles once the game has sent the event

document.querySelector("#buy").addEventListener("click", async () => {
try {
await KcdMp.emitServer("shop.buy", { item: "bread", count: 1 });
} catch (e) {
KcdMp.log.warn("could not send: " + e.message);
}
});

KcdMp.onServer · KcdMp.post · the Messages and events group of the index