Multiplayer
Players can play together in a room. One player creates a room and gets a short code such as K7QMD. Friends join with that code. Everyone in the room can then send messages to the others, and every other player gets them at once.
The room only passes messages along. It never runs your game. So the rule of thumb is: each player moves their own objects and tells the others where they are, and the host decides the things that must be the same for everyone (the score, who won, when a round starts).
With event blocks
Category Multiplayer.
| Block | Does |
|---|---|
| Create room | Makes a room as Your name, for up to 2 to 8 players. |
| Join room | Joins the room with a code a friend gives you. |
| Leave room | Disconnects. |
| Send message | Sends a named message with one value (number or text) to every other player. |
| On room joined, On room failed | Triggers for the room you made or tried to join. On failed, neterror() says why: notfound, full, busy or offline. |
| On player joined, On player left | Triggers when someone else comes or goes. netwho() is their name. |
| On message | Trigger for a message another player sent, for one name or any (leave the name empty). |
| Message is | Condition for a sub-event under an On message with an empty name: only goes on when the message has this name. |
| On connection lost, On reconnected | The connection dropped and the game is getting back in by itself; then it is back (see When the connection drops). |
| On disconnected | The connection to the room was lost for good. neterror() says why. |
| Quick match | Joins the fullest open room, or makes a public one for the next player. |
| Set shared value, Add to shared value | A value the whole room shares and remembers (see below). |
| On shared value changed | Trigger: someone changed a shared value, or you just joined and it was already set. |
| Take control | You drive this synced object from now on (see below). |
| Is mine | Condition: this player drives the object. |
| I am the host, In a room | Conditions. |
Expressions: netshared("name"), netcode() (show it so friends can join), netplayers(), netishost(), netmyname(), netwho() (the player of the latest join, leave or message), netmessage(), netdata(), netdata("x") (a part of what the message carries), netplayername(i) and netplayerid(i) (go through the players with a loop), nethostname(), neterror().
Send message carries up to three values. One value arrives as it is; two or three arrive as a list, read with netdata("0"), netdata("1"), netdata("2").
For example, two players, each with an object the other one sees as Friend: Create room (and the other player Join room); then under On room joined, set a Text to "Code: " + netcode(). Under Every 0.1 seconds, Send message pos with Player.x and Player.z. Under On message pos, Set position of Friend to (netdata("0"), 0, netdata("1")).
To handle many messages with one trigger, leave the name of On message empty and add a Message is sub-event for each name (coin, reset, ...). netdata() and netwho() work inside each sub-event.
In scripts
const r = await game.net.create({ name: 'Ann', max: 4 });
if (r.ok) game.log('Tell your friends: ' + r.code);
// a friend
const j = await game.net.join('K7QMD', { name: 'Bo' });
game.net.send('pos', { x: 1, y: 0, z: 2 }); // to everyone else
game.net.send('whisper', 'hi', somePlayer); // to one player
game.on('netMessage', (m) => { /* m.from.name, m.name, m.data */ });
game.on('netPlayerJoined', (e) => game.log(e.player.name + ' joined'));
game.on('netPlayerLeft', (e) => game.log(e.player.name + ' left'));
game.net.players, .me, .host, .isHost, .code and .state (offline, connecting, online, reconnecting) tell where you are. Nothing throws: a room that cannot be joined is ok: false with an error. The editor's Snippets has a Multiplayer room (global) example.
Syncing objects
Sending positions by hand works, but most games just want "this object is shared". Two things do that for you.
Spawn for everyone (block) or game.net.spawn('Car', { x: 0, y: 1, z: 0 }) (script) makes a prefab that every player in the room sees. You drive it: its position goes to the others about ten times a second, and on their side it is a copy that glides to where yours is. When you destroy it, or leave the room, it goes for everyone. A player who joins later gets the objects that already exist. Outside a room it simply makes the object, so the same game also plays alone.
The Net sync component (Add component → Net sync) chooses what is sent: updates per second (1 to 30), and whether position, rotation and scale travel. A prefab without it still syncs position and rotation at 10 a second. Put it on an object placed in the scene and that object belongs to the host; the other players see it glide.
Things to know:
- Whose is it? Every synced object has one driver, and Is mine tells you whether that is you. See Who drives an object below: it is the most important idea in multiplayer.
- Copies stand still on their own. The behaviors of an object someone else drives do not run on your side (their Platformer would otherwise walk to your joystick), and its physics body follows what it is told. Your own scripts and events still run, so check Is mine in anything that reads input.
- Smooth motion. Positions go out with the owner's clock, and the others draw the object a moment (about 1.6 update intervals, 0.16 s at the default 10 a second) in the past, gliding between the two positions around that moment. So movement stays smooth even though only ten updates a second travel. If you need it tighter for something fast, raise Updates / s on the Net sync component (up to 30; each update costs a message).
- Only the owner can move an object: a position sent by anyone else is ignored.
- Physics bodies keep their speed. When control of a physics object changes hands (Take control on a ball), the new driver's copy carries on at the speed and spin the object had, worked out from its last two updates. A platformer-style character slows down again by itself, as usual.
- Movements smaller than half a centimetre are not sent, and an object that stands still sends one update a second.
Who drives an object
In a room every player runs their own copy of the game, on their own device. Every event and every script runs on every device. So for each synced object the room needs one answer to whose job is it to move this? That player is the object's driver. On the driver's device the object is the real one: it moves, it collides, and it sends its position. On every other device it is a copy that glides to where the driver says it is.
| The object | Its driver |
|---|---|
Made with Spawn for everyone (game.net.spawn) | The player who spawned it |
| Placed in the scene, with a Net sync component | The host |
| After somebody uses Take control on it | That player |
| Anything else (no Net sync, not spawned for everyone) | Nobody: it is not shared. Every player has their own copy of it, and each is free to do what they like with it. |
| Any object, when you are not in a room | You |
Whose object is it
Name labels, team colours and score boards are yours to design, so the engine gives you the facts and no label. Any object has two properties: ownerName is the name of the player who drives it (empty when nobody does), and ownerId is that player's id (0 when nobody does). To label a player, give the player's prefab a 3D text child and set its text to Self.ownerName once the object exists; or read Ball.ownerName from a UI text. In a script it is entity.owner?.name.
netping() (game.net.ping in a script) is the round trip to the room in milliseconds, a new reading every few seconds, and 0 when you are not in a room. Put it in any text you like.
Is mine
Is mine (the condition, or entity.isMine in scripts) is true when this player is the driver of the object. It is:
truefor the objects you drive, on your device;falsefor the copies of other players' objects;- always
truefor objects that are not synced, and for every object outside a room. So a game that also plays alone needs no special case: put Is mine in and it just works.
entity.owner is the same thing as a player: the driver as { id, name, isHost, isMe }, or null outside a room. Use it for a name tag or a message: "Ann's car".
Why it matters
Say the player moves a ball with the joystick: Every tick → Set move input Ball to the joystick. In a room there are several balls named Ball on your device: yours, and a copy of each friend's. The event would steer all of them with your finger. (A copy does not run its behaviors, so it would not move by itself, but the event still pushes it.) The fix is one extra condition:
- Every tick, Ball is mine → Set move input
Ballto the joystick.
Ball is mine keeps only the balls you drive, and the action then works on just those. In a script:
function update(dt) {
if (!entity.isMine) return; // a copy of somebody else's: they move it, not me
entity.translate(input.axis.x * 5 * dt, 0, -input.axis.y * 5 * dt);
}
The rule of thumb: anything that reads the player's input, or decides what happens to an object, goes behind Is mine. That includes hits: a bullet that touches a target is seen on every device, so let only the bullet's driver decide "that was a hit" and tell the others (a shared value or a message). Do not let each device decide on its own: a copy is about a fifth of a second behind the real thing, so devices would disagree. The Ball Arena showcase does it this way with coins.
Taking control
A driver is not for ever. Take control on an object (or entity.claim()) makes you its driver from then on: you send its position, and everyone else, the old driver included, sees a copy. Use it for a ball somebody picks up, a car they get into, a crate they push. If two players ask in the same moment, the room's order decides, and it is the same on every device. After it, Is mine is true for the new driver and false for everybody else.
- A placed object goes back to the host when its driver leaves.
- An object a player spawned disappears when that player leaves, unless somebody else has taken control of it by then.
- Take control needs the object to be synced (a Net sync component, or spawned for everyone). On any other object it does nothing, because there is no one copy to hand over.
Shared values
Messages only reach the players who are there when they are sent. For things a late joiner must also know (the score, the round, which coins are taken) use a shared value. The room remembers it and sends the current ones to everyone who joins.
- Set shared value
roundto3, orgame.net.setShared('round', 3). A value is a number, text, or a small list or object (about 500 characters); there can be 64 of them. Setting it to nothing (null) removes it. - Add to shared value
score1, orgame.net.addShared('score', 1): safe when two players add at the same moment, because the room does the adding. - On shared value changed
round(orgame.on('netShared', e => ...)) fires on every player when it changes, you included, and once for each value that was already set when you joined. Read it withnetshared("round")orgame.net.getShared('round').
Changes come back in the room's order, the same for everybody, so you see your own change a moment after you make it. Anyone can change a shared value, so, as with messages, check what is sensible before you rely on it.
Finding a game
Quick match (or await game.net.quickMatch({ name })) looks at the open rooms of your game and joins the fullest one that has space. If there is none it makes a public room for the next player to find. You can list rooms yourself: const rooms = await game.net.listRooms() gives { code, players, max, title } for each open one. To list a room you make, tick Listed for Quick match on Create room, or game.net.create({ public: true, title: 'Friday game' }).
A room everyone lands in
Create room and game.net.create take an optional room code of your own (3 to 12 letters or digits): game.net.create({ code: 'ARENA' }). The first player makes the room; anyone else who uses the same code joins it. That gives you a ready-made lobby without typing codes, as in the Ball Arena demo (showcase/netBalls.ts in the engine repo).
The host
The host is the player who has been in the room the longest. When the host leaves, the next player becomes host and everyone is told. Use it for decisions that must come from one place: only the host spawns the enemies, only the host counts the score.
When the connection drops
Phones lose their connection all the time: switching from Wi-Fi to mobile data, going into a tunnel, a few seconds in the background. The game gets back into the room by itself:
- On connection lost (
netReconnecting) fires andgame.net.stateisreconnecting. Messages and shared changes sent now are dropped. Your objects stay where they are, for you and for the others. - The relay keeps your seat for 20 seconds. The other players are not told you left; your objects simply stand still for them.
- Back in: On reconnected (
netReconnected) fires. You are the same player (same id, still host if you were), and you still drive the same objects. Whatever you missed arrives as the usual events: On player left / On player joined for anyone who came or went, On shared value changed for values that changed. On room joined does not fire again, so a game that spawns its player there does not spawn a second one. - Not back within the 20 seconds (or the room closed): On disconnected fires with
neterror()=gone(oroffline). For everyone else you then leave the room, as if you had pressed Leave.
A game that wants to show it: a "Reconnecting…" text that On connection lost shows and On reconnected hides.
A player who leaves properly (Leave room, closing the game) leaves at once, without the 20 seconds.
Every player is trusted
The room never runs your game, so it cannot check what players do. Any player could, with a modified copy of your game:
- send any message, with any data, as often as the limits allow;
- set or add to any shared value;
- Take control of any synced object and move it anywhere.
That is fine for playing with friends, for co-op and for party games. It is not enough for competitive games with rankings or prizes: those need a server that runs the rules, which this relay deliberately is not. Within a game, keep it fair the usual way: let the host decide results, check that numbers coming in are sensible (a speed, a distance moved since the last message), and never put secrets in messages or shared values, since every player in the room sees them.
Limits
These keep the shared relay affordable for everyone:
- At most 8 players in a room, and 64 shared values of about 500 characters.
- A message is at most about 2000 characters, and a player can send 30 messages and shared changes a second together. Ten a second is plenty for positions; more are dropped, with one warning in the console. Don't add to a shared value every frame: add up locally and send the total now and then.
- A room closes after 3 hours.
- The relay has a daily limit on joins across all games, and one game can use at most half of it. One player can join 150 times a day. When a limit is reached, joining says
busyuntil the next day (midnight UTC); rooms already open keep going.
Rooms are separate for each game, and a room code only works in the game that made it.
Your own relay
Project settings → Multiplayer → Server is empty by default (the engine's relay). If you run your own relay, put its wss:// address there and the game uses it instead. A game published to the gallery can always reach the server you set.