Commands
Updated
Every action a person takes in a room is a command: sending a reaction, raising a hand, locking the room, removing someone. A script runs a command with Network.command, and Arena checks it against the room’s rules before carrying it out: the rules decide who may run each command, so a guest cannot lock a room that only its host may lock. Some commands are relayed to the other peers, which react to them with Network.onCommand. Use commands to build moderation panels, reaction buttons and host tools.
Before you start
Section titled “Before you start”- You know what a room and a peer are. See the Multiplayer overview.
- You know the session members of
Network, such asNetwork.connectedandNetwork.peers. See Network. - Commands return promises: call them from an
asyncfunction andawaitthem.
Running commands
Section titled “Running commands”| Member | What it does |
|---|---|
Network. |
Run the command name with args (an object), aimed at the peer target when the command acts on someone. Resolves the room’s result; rejects with an error that has a code when the room refuses. |
Network. |
Whether the room’s rules let this peer run name now; false while not in a room. |
Network. |
Call handler(args, from) for each command named name that another peer runs and that reaches this peer. from is that peer’s id. Returns a function that stops it; it also stops when the script is released. |
- Permissions change live. What a peer may do arrives when it joins and changes when the room’s rules or the peer’s roles change. Read
canwhen you need it rather than once at startup. canis a hint, not a guard. Arena checks every command again, so a refused command still rejects. Usecanto show or hide buttons.- Commands the runtime sends itself. Registering, spawning, claiming and destroying objects are commands too. MIRA sends them for you when you replicate, spawn or take objects.
NetworkCommand
Section titled “NetworkCommand”NetworkCommand names the commands, so you do not type strings. Each key is the command in PascalCase: NetworkCommand.RoomLock is 'room.lock', NetworkCommand.ParticipantHandRaise is 'participant.hand.raise', NetworkCommand.MediaStopScreen is 'media.stop-screen'.
| Group | Keys |
|---|---|
| Room | RoomLock, RoomUnlock, Room, RoomEnd, RoomReset, Room, Room, RoomHostClaim, RoomAnnounce, RoomFollowMe |
| Moderation | Participant, Participant, Participant, Participant, Participant, Participant, Participant, Participant, Participant |
| Presence and social | Participant, Participant, Participants, ChatAttend, ReactionSend, Participant, Participant, Participant |
| Media | MediaMute, MediaUnmute, MediaMuteAll, Media |
| Objects | Object, ObjectSpawn, ObjectDestroy, Object, ObjectClaim, Object, ObjectRelease |
A room can offer commands of its own beyond these; run them by name, such as Network.command('emoji.burst', { emoji: '🎉', count: 5 }).
Relayed commands
Section titled “Relayed commands”Some commands reach the other peers, which hear them through onCommand: room.announce and reaction.send, for example. A few are requests for the receiving peer to carry out: media.mute, media.unmute, media.stop-screen, participant.summon, participant.teleport and room.follow-me. MIRA does not act on these by itself. If your scene should obey them, handle them with onCommand and do the work in your script.
When the room refuses
Section titled “When the room refuses”A refused command rejects with an error whose code says why:
| Code | Meaning |
|---|---|
400 |
Unknown command, or wrong arguments. |
401 |
This peer is not in a room. |
403 |
The room’s rules do not allow it, or the target ranks higher. |
404 |
No such target. |
409 |
A conflict, such as resetting a room while others are in it. |
429 |
Sent too often. |
Some mistakes throw a TypeError at once, before anything is sent: a missing name, args that are not an object, or an empty target. A handler passed to onCommand that throws is reported to its script, and the other handlers still run.
Resetting a room
Section titled “Resetting a room”NetworkCommand.RoomReset empties the room: every object it holds or keeps. It runs only while the sender is alone in the room (409 otherwise), and rooms usually do not allow it. The peer that resets keeps its own objects and registers them again, so the scene it plays starts the room anew. MIRA never resets a room on its own.
Examples
Section titled “Examples”1. A reaction button
Section titled “1. A reaction button”#pragma lifecycle(startup)
function startup() { on('react/thumbs', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; if (!Network.connected) { log('Reactions need a room'); return; } try { await Network.command(NetworkCommand.ReactionSend, { reaction: '👍' }); } catch (error) { if (error.code === 429) log('Slow down'); else log('Reaction refused:', error.code); } });
Network.onCommand(NetworkCommand.ReactionSend, (args, from) => { log(`${from} reacted ${args.reaction}`); });}A UI Button sends a thumbs-up reaction to the room with Network.command, handles a refusal by its code, and logs the reactions other peers send.
Press the button. The preview has no room, so it logs that reactions need one; in a published world every peer sees your 👍.
commandresolves when the room accepts the reaction and rejects with acodewhen it does not.onCommandhears the reactions other peers send, with the sender’s peer id infrom.
Snippets
Section titled “Snippets”1. A host panel that only shows what you may do
Section titled “1. A host panel that only shows what you may do”#pragma lifecycle(startup)
// Scene: a "Host Panel" UI Panel holding a "Lock" UI Toggle Button with a// Signal Emitter on 'host/lock', and a "Lower Hands" UI Button with a Signal// Emitter on 'host/hands'. A Networkable has a Signal Emitter on 'net'.let panel;let lockButton;let handsButton;
function refresh() { lockButton.visible = Network.can(NetworkCommand.RoomLock); handsButton.visible = Network.can(NetworkCommand.ParticipantHandLowerAll); panel.visible = lockButton.visible || handsButton.visible;}
function startup() { panel = findEntity('Host Panel'); lockButton = findEntity('Lock'); handsButton = findEntity('Lower Hands'); refresh();
on('net', (event) => { if (event.eventType === EventType.Networkable.Connect) refresh(); });
on('host/lock', async (event) => { if (event.eventType !== EventType.UIToggleButton.Change) return; const name = event.data.checked ? NetworkCommand.RoomLock : NetworkCommand.RoomUnlock; try { await Network.command(name); log(event.data.checked ? 'Room locked' : 'Room unlocked'); } catch (error) { if (error.code === 403) log('Not allowed'); } });
on('host/hands', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; await Network.command(NetworkCommand.ParticipantHandLowerAll); });}candecides which buttons are visible; permissions arrive with the join, so the panel is refreshed onconnecttoo.- The toggle button runs
RoomLockorRoomUnlockdepending on its new state. - Arena checks each command again, so the
403branch still matters.
2. Obeying summon and announce
Section titled “2. Obeying summon and announce”#pragma lifecycle(startup)
// Scene: an "XR Origin" or player root entity named "Player", and a "Banner"// UI Label that shows announcements.let player;let banner;
function startup() { player = findEntity('Player'); banner = findEntity('Banner').getComponent(UILabelComponent);
Network.onCommand(NetworkCommand.RoomAnnounce, (args, from) => { banner.setProps({ text: `${args.message}` }); log('Announcement from', from); });
// MIRA does not move the player by itself: this script decides what a summon does. Network.onCommand(NetworkCommand.ParticipantSummon, (args, from) => { log(`${from} summoned me`); player.setPosition(0, 0, 2); });}- Relayed commands arrive through
onCommandonly; the runtime leaves the reaction to your script. - What a summon does is your choice here: the player is moved to a gathering point.
3. Moderating one person
Section titled “3. Moderating one person”#pragma lifecycle(startup)
// Scene: a UI Button with a Signal Emitter on 'mod/kick-latest'.async function kick(peerId) { try { await Network.command(NetworkCommand.ParticipantKick, {}, peerId); log(`Removed ${peerId}`); } catch (error) { if (error.code === 403) log('You cannot remove this person'); else if (error.code === 404) log('They already left'); else log('Kick failed:', error.code); }}
function startup() { on('mod/kick-latest', (event) => { if (event.eventType !== EventType.UIButton.Press) return; if (!Network.can(NetworkCommand.ParticipantKick)) return; const newest = Network.peers[Network.peers.length - 1]; if (newest) kick(newest); });}- A command that acts on someone takes that peer’s id as the third argument.
Network.peerslists the other peers oldest first, so the last one is the newest arrival.404means the target is gone;403that the rules forbid it or the target ranks higher.
Common mistakes
Section titled “Common mistakes”- Trusting
canalone. It reflects the rules when you read it; the room may refuse anyway. Always handle a rejection. - Expecting the runtime to obey relayed requests.
media.mute,participant.summon,participant.teleportand the like do nothing unless a script handles them. - Passing a string as
args.argsmust be an object, even when empty:Network.command(name, {}, peerId). - Running commands while alone and offline. Outside a room every command rejects with
401. CheckNetwork.connectedfirst.