Mugon.start and the context
The entry point of every game and everything the context offers.
Loading the SDK
With a bundler:
// The SDK must be imported first: importing it says hello to the platform.
import { Mugon } from "@mugon/sdk";
Mugon.start(async (ctx) => {
console.log(`running as ${ctx.role} with id ${ctx.self.id}`);
});Without a bundler (Unity, Godot, Rust/wasm shells, plain JS) use the self-contained
mugon.iife.js from the package (node_modules/@mugon/sdk/dist/mugon.iife.js) as the first
script of your index.html:
<script src="./mugon.iife.js"></script>
<script type="module" src="./main.js"></script>It defines a global Mugon with Mugon.start and Mugon.Channel.
If the platform doesn't hear from your game within 20 seconds it shows "The game did not respond". The usual cause is a game that doesn't load the SDK, or loads it too late.
Mugon.start(callback)
Mugon.start waits for the platform's context, runs callback(ctx) and returns a promise that
settles when the callback does. Call it once. Outside a mugon iframe it rejects.
When the callback's promise resolves, the platform treats the game as loaded and closes its
loading overlay. If your game keeps running inside the callback, call
ctx.progress.done() yourself, see loading progress.
The context
ctx is frozen and only exists inside the callback. The SDK also publishes it as
window.mugon before the callback runs, for code that can't be handed it (a wasm module, see
Bevy).
| Member | Meaning |
|---|---|
role | "server" (the host) or "client". |
self | This player, a { id, name }. |
server | The server player. Equals self on the server. |
initialPlayers | Players connected when the game started. Later joins and leaves arrive as playerConnected and playerDisconnected events. A client's initialPlayers always contains the server. |
settings | The values the lobby chose for the settings your game version declares in mugon.toml, keyed by setting key. Booleans, numbers, strings, or string lists, matching the declared type. Empty if the game declares none. |
getBoolean(key) / getNumber(key) / getString(key) / getStrings(key) | Typed reads of a setting; undefined if absent or declared with another type. |
send(playerId, data, channel?) | Send a Uint8Array to a player. |
disconnectPlayer(playerId) | Disconnect a player. |
on(event, handler) | Subscribe to "playerConnected", "playerDisconnected" and "message". Returns an unsubscribe function. |
showInvite() | Ask the platform to show the lobby invite options, including a join QR code. |
endGame() | Host only. End the game and send everyone back to the lobby, see ending the game. Needs SDK 0.2. |
requestNextRound(settings, { carryOver }?) | Host only. Ask the host to start another round on settings, optionally handing a string to the next round. Needs SDK 0.2 (named nextGame before 0.3). |
carryOver | Host only. The string the previous round passed to requestNextRound, or undefined, see carrying state over. |
toggleFullscreen() | Toggle fullscreen (call it from a click handler). |
progress.set(fraction, label?) | Report loading progress. |
progress.done() | Say the game is playable. |
Players
A player is { id, name }. The name is the player's display name, set by the platform: a game
cannot forge it and a client cannot choose another one. It is 2 to 32 characters long, can contain
any Unicode, is not unique and is a snapshot from the moment the player joined. Identify
players by id, and escape name when you put it into HTML.
You only see the players you are connected to: the host sees every client, a client sees the host. If every client should show every name, have the host send the list over the network.
Events
ctx.on("playerConnected", (player) => { /* a player joined: player.id, player.name */ });
ctx.on("playerDisconnected", (playerId) => { /* a player left */ });
ctx.on("message", ({ from, channel, data }) => { /* from is a player id; data is a Uint8Array */ });Events that arrive before you subscribe are queued and delivered to your first handler, so a game that loads for a while doesn't lose messages.
Sending
import { Channel } from "@mugon/sdk";
ctx.send(playerId, new Uint8Array([1, 2, 3]), Channel.UnreliableUnordered);Channel is one of ReliableOrdered (the default), ReliableUnordered, UnreliableOrdered
and UnreliableUnordered. send transfers the buffer of the array you pass, which detaches
it. Pass a copy if you still need the data.
Ending the game
Both calls are for the host (role === "server"). On a client they do nothing but log a warning.
// the winner is known: everyone goes back to the lobby right away
ctx.endGame();
// another round, e.g. after a "vote for the next map" screen
ctx.requestNextRound({ ...ctx.settings, map: "desert" });requestNextRound does not start anything by itself. The host sees "Continue to next round?" and
chooses between Continue and Back to lobby. On Continue every player is moved into a
fresh game, which starts with your new settings in ctx.settings.
settings is the complete set of values and replaces the lobby's, it is not a patch. Start from
ctx.settings, change what the next round needs and pass the whole object back. A setting you leave
out gets its declared default, like when a game starts from the lobby.
If the platform rejects the values (a key your mugon.toml doesn't declare, a value of the wrong
type or outside the allowed range, or a combination that settings-valid-when forbids), nobody
starts a new round: the game ends and everyone is sent back to the lobby, and the host is told that
continuing failed. Check your settings against your declarations.
Carrying state over to the next round
ctx.requestNextRound(
{ ...ctx.settings, map: "desert" },
{ carryOver: JSON.stringify({ scores }) },
);
// in the next round, on the host:
const scores = ctx.carryOver ? JSON.parse(ctx.carryOver).scores : {};carryOver is a string of your choosing, up to 1 MiB (counted in UTF-8 bytes). Treat it as
optional and untrusted:
- Only the host's game receives it, in the round right after. A client's
ctx.carryOveris alwaysundefined, so send whatever clients need over the network. - It lasts for one round. If the host chooses Back to lobby, the game ends, the settings are
rejected or the host reloads the page, the next round starts with
undefined. - It stays on the host's device and is never stored by the platform, but it is not secret: it passes through the host's page like the rest of your game.
- A string over the limit drops the whole
requestNextRoundcall and logs a warning.
Words used in these docs
- A player is a participant in the game, the host included. A player is a
{ id, name }. - Server and client are the part a player takes in the game's network (
ctx.role). - The host is the person who started the lobby; their game frame is the server.
- A round is one run of the game. The lobby outlives its rounds.