Repository navigation
Worlds
An Engine runs one or more Worlds. Each World is a separate game: its own nodes and scenes, its own systems, its own clock and physics. A single-player game has one World and never has to think about this. Multiplayer games, split-screen, a menu that starts a match, or a background simulation use several.
The design is in ADR 0032 and ADR 0034. This guide is about using it. For networking between Worlds, see Multiplayer.
A project without a bootstrap section in project.json gets the default bootstrap: one World named Default, tagged default and local. It renders, takes local input, and runs every system whose worlds filter matches its tags (by default, all of yours). This is how the Flappy Bird sample runs.
engine.world is the Engine's default World: the first one its bootstrap made. If that World is destroyed, the next one becomes the default.
A World has a name, unique in its Engine, and a list of tags. Tags are free strings. These have meaning to the engine:
| Tag | Meaning |
|---|---|
default |
The ordinary game World. Systems without a worlds filter run here. |
local |
A World that simulates on its own, not over a network. Systems without a worlds filter run here too. |
client |
A network client (see Multiplayer). |
server |
A network server. Doesn't render unless its config says renders: true. The dedicated server runs only these. |
editor |
The editor's own World. Game systems don't run here unless their filter names editor. |
You can add your own (ai, replay, ui) and filter systems and components by them.
To run more than one World, add a bootstrap section to project.json. The editor's Worlds panel writes it for you (see below); this is what it looks like:
"bootstrap": {
"worlds": [
{ "name": "Server", "tags": ["server"], "tickRate": 30, "scenes": ["scenes/arena.scene.json"] },
{ "name": "Client", "tags": ["client"], "scenes": ["scenes/arena.scene.json"], "systems": { "exclude": ["game.DebugDraw"] } }
],
"links": [{ "from": "Client", "to": "Server", "transport": "loopback" }],
"templates": {
"Match": { "tags": ["server"], "scenes": ["scenes/arena.scene.json"] }
}
}-
worlds: the Worlds created at start, in order. The first is the default World. Each entry takes:-
name(required) andtags(default["default", "local"]). -
tickRate: fixed steps per second. Default:1 / time.fixedDeltaTimefromproject.json(60). -
timeScale: the starting time scale. Default 1; 0 starts paused. -
rendersandinput: whether it wants the screen and local input. See The view and input. -
scenes: scenes loaded at start. The first replaces, the rest load additively. -
systems:includeandexcludelists of system names, on top of what the tags select.
-
-
links: network links,froma client Worldtoa server World. Each applies once both Worlds exist: at start for two start-up Worlds, or when game code creates a Spawnable World the link names (see Links to Spawnable Worlds). See Multiplayer. -
templates: Spawnable Worlds, named World configs that game code creates when it needs them. Same keys as a World entry;nameis optional and defaults to the template's key. The editor calls them Spawnable Worlds; inproject.jsonand the API they'retemplates.
The bootstrap is checked when the Engine starts. Unknown keys, duplicate names, bad tick rates, unknown system names and links to missing Worlds are errors. You get one error that lists every problem, and no World is created.
For setups the declarative form can't express, point bootstrap at a project script:
"bootstrap": "scripts/Bootstrap.ts"The script exports a function named bootstrap that creates the start-up Worlds:
import type { Bootstrap } from "myco";
export const bootstrap: Bootstrap = (engine) => {
const server = engine.createWorld({ name: "Server", tags: ["server"], scenes: ["scenes/arena.scene.json"] });
const client = engine.createWorld({ name: "Client", tags: ["client"], scenes: ["scenes/arena.scene.json"] });
client.connect(server, "loopback");
};The editor and the dedicated server load the script themselves. Code that calls Engine.create directly passes the function instead: Engine.create({ bootstrap }). A bootstrap can be async.
defineSystem registers a system in a catalog. It doesn't run anywhere by itself. A World runs the catalog systems whose worlds filter matches one of its tags:
import { defineSystem } from "myco";
defineSystem({
name: "game.ApplyDamage",
phase: "fixedUpdate",
worlds: ["server", "local"], // the authoritative game, online or offline
run(rows, ctx) {
// …
},
});- Without
worlds, the filter is["default", "local"]. So a system you write for a single-player game runs in the default World, but not in a World tagged onlyclientorserver. Give multiplayer systems an explicit filter. -
"*"matches every World. Engine built-ins (script hooks, physics, networking) use it. - A World can add or drop systems by name:
systems.include/systems.excludein its config, oraddSystem(name)/removeSystem(name)on its handle at runtime. - Order (
phase,after,before) works within each World's own list. - Scripts loaded later (in the editor, say) join the Worlds whose tags match.
handle.systems() lists a World's systems in execution order. The Worlds panel shows the same list with the reason for each one.
GameBehaviors run in every World where their component exists. By default a component exists in every World that loads its scene. To keep one out of some Worlds, give it a filter:
import { GameBehavior, behavior } from "myco";
@behavior("game.EnemyAI", { worlds: ["server", "local"] })
export class EnemyAI extends GameBehavior {
update() {
// runs only where the game is simulated, never on a client
}
}A scene file can also set "worlds" on one component entry, for that instance only (it works for data components too). In Worlds that don't match, the component isn't created when a scene or prefab loads. The editor's own World creates everything, so you can still edit it.
Each World has its own clock. Time reads the clock of the World that's running:
-
Time.deltaTime,Time.time,Time.fixedDeltaTimeand the rest are that World's. -
Time.timeScalescales that World only. At 0, its fixed steps stop andupdateruns withdeltaTime0. -
tickRatein a World's config sets its fixed step. A 30 Hz World runsfixedUpdateon about every other frame of a 60 Hz display; a 120 Hz one runs it twice per frame. - In a behavior,
this.timeis its own World's clock. In a system,ctx.timeis. -
Time.network(andthis.time.network,ctx.time.network) is set on networked Worlds. See Multiplayer.
The Engine has one loop. Each frame it steps every running World once, in creation order. Each World turns the frame's real time into as many fixed steps as its accumulator and tick rate call for, capped at time.maxSubSteps.
Game code can create Worlds while it runs, for example from a menu's Host button. Engine.createWorld takes a World config or a template name and returns a World handle:
import { Engine, GameBehavior, Input, behavior } from "myco";
@behavior("game.MainMenu")
export class MainMenu extends GameBehavior {
update() {
if (!Input.getKeyDown("enter")) return;
const server = Engine.createWorld("Match"); // the template from project.json
const client = Engine.createWorld({ name: "Player", tags: ["client"], scenes: ["scenes/arena.scene.json"] });
client.connect(server, "loopback");
void Engine.findWorld("Menu")?.unload(); // this menu's own World; the screen and input pass to "Player"
}
}- The new World's scenes load in the background, and it steps from the next frame.
- A template creates a World named after the template (or its
name), so it can exist once at a time. For several, pass configs with different names. - A bad config throws an error listing every problem, and nothing changes.
-
Engine.createWorldandEngine.findWorldact on the Engine of the World that's running. In setup code that holds the Engine,engine.createWorldandengine.findWorlddo the same.
A link can name a Spawnable World. It applies when game code creates that World, if the World at the other end is alive: the client connects to the server as if the script had called connect right after creating it. With this bootstrap, a menu's Host button only has to create the server:
"bootstrap": {
"worlds": [{ "name": "Client", "tags": ["client"], "scenes": ["scenes/menu.scene.json"] }],
"links": [{ "from": "Client", "to": "Server", "transport": "loopback" }],
"templates": { "Server": { "tags": ["server"], "renders": false, "scenes": ["scenes/arena.scene.json"] } }
}Engine.createWorld("Server"); // the link connects Client to it- Links name Worlds, by name:
Engine.createWorld("Server")completes links namingServer, but a copy made ascreateWorld({ ...templates.Server, name: "Server 2" })completes only links namingServer 2. - If the server World is destroyed and created again, its links apply again.
- A script's
connectfor a pair a link already connected does nothing, with a warning. - The Arena sample works this way: Host creates
Server, and P (a second local player) createsClient 2; both connect through links.
A WorldHandle has:
| Member | What it does |
|---|---|
name, tags, hasTag(tag)
|
The World's identity. |
alive |
False from unload() on. Methods on an unloaded handle throw. |
state |
What the World does: held, activating, playing, paused, unloading or destroyed (below). |
loading, ready, holdPending, loadProgress
|
Whether a load is in flight, whether the latest reached readiness, whether the next replacing load will hold, and how far the current load has come. |
load(path, { additive, hold, signal, onProgress }) |
Loads a scene into that World. Resolves at the swap. |
whenReady() |
Resolves when nothing is loading and the latest load is ready. |
activate() |
Releases a held World: it runs its deferred start() calls hidden, a few milliseconds a frame, then plays. |
pause({ audioFadeMs }), resume()
|
Stops and restarts a World nobody is looking at. |
unload() |
Tears the World down, fading its sounds; resolves once it's gone. The Engine's last World can't be unloaded. |
onChange(listener) |
Called after each change of state (at the end of the World's frame), loading, ready or holdPending (when it changes). |
addSystem(name), removeSystem(name), systems()
|
Its system list. |
connect(server, transport, options) |
Joins it to a server World as a client. See Multiplayer. |
loadScene, paused, destroy()
|
Deprecated: use load, state === "paused" and unload(). destroy() still tears down at once, without the fade. |
A World is always in one state (ADR 0615):
| State | Steps systems and scripts | New content's start() and play-on-awake |
Sound |
|---|---|---|---|
held |
no | waits for activate()
|
silent |
activating |
no |
start() runs, a slice a frame |
silent |
playing |
yes | yes | heard when it's on screen |
paused |
no | waits | paused in place |
unloading |
no | no | fades out |
-
Loading doesn't change the state. A playing World that loads keeps playing;
loadingsays a load is in flight. -
Holding lets a World load behind another screen without starting:
-
load(path, { hold: true }), orWorldConfig.start: "held"for its first replacing load, entersheldat the swap. -
awakeandonEnablerun then.start(), the systems and play-on-awake sounds wait. -
activate()runs thestart()calls hidden, withinactivationBudgetMsa frame (default 4), and then the World plays.
-
- A World that doesn't step stays connected: the Engine keeps its network alive while it's held or paused.
-
Behaviours hear about pauses:
onWorldPause()andonWorldResume(), called once at each transition. - Server Worlds can't be paused or held: it would drop every client.
-
Real time keeps going:
Time.realtimeSinceStartupis the same in every World, paused or not. Game time freezes in every state butplaying. -
Async code keeps running: an
awaitor asetTimeouta script started still continues while its World is paused or held. Checkthis.world.statein such code, or use the engine's waits, which follow game time.
A pause menu is not pause(). Pausing a World stops its input and UI too. For a pause menu, set Time.timeScale = 0: the World keeps stepping, so its menu animates and takes input. pause() is for a World nobody is looking at, such as the menu behind a match.
const match = Engine.createWorld({ ...templates.Match, name: "Match", start: "held" });
await match.load("scenes/arena.scene.json");
await match.whenReady(); // loaded, warmed and lit, still hidden
await match.activate(); // its start() calls ran; it plays from the next frameWorlds don't share nodes. A handle is the only thing one World can hold of another, and it never leads to the other World's nodes.
- Reparenting a node under another World's node throws. So do
GameObject.createandinstantiatewith a parent from another World,$nodereferences and initial values that point across Worlds, and buffer records holding another World's nodes. - Plain field assignment can't be checked. Don't store another World's nodes in fields.
-
Inputreturns nothing in a World that doesn't take input. -
SceneManager,GameObject.instantiate,Physics2Dand the other static facades act on the World that's running. - Assets and storage are shared by the Engine. Autoloads (
project.json'sautoload) go into the default World only. - Script modules are shared too: a module-level variable is the same in every World of the page. Keep per-World state in components.
Worlds exchange data only through network messages: RPCs, inputs and replication over a link, even inside one Engine.
While a World's frame runs, that World is current, and the static facades act on it. Between frames, the Engine's default World is current.
JavaScript can't carry the current World across an await. Code after an await runs after the frame has ended, so a bare GameObject.instantiate(path) or Time.time there acts on the default World, which may not be yours. Use the forms that name their World:
-
this.instantiate(path, options)in a behavior: always its own World. A parent from another World throws. -
this.time: always its own World's clock. -
GameObject.create(name, { parent })andGameObject.instantiate(path, { parent }): the parent's World. -
handle.load(path): the handle's World.SceneManager.loadcaptures the current World when you call it, so call it before theawait, not after.
import { GameBehavior, behavior } from "myco";
@behavior("game.Spawner")
export class Spawner extends GameBehavior {
async start() {
await this.wait.seconds(2);
this.instantiate("prefabs/enemy.prefab.json"); // right World, even if it isn't the default one
}
}In a single-World game, both forms behave the same.
An Engine has one screen and one set of local input devices. The first World that wants the view gets it: every World does unless its config says renders: false, except server-tagged Worlds, which want it only with renders: true. Input follows the view unless a World's config sets input itself. When the World with the view is destroyed, the view passes to the next World that wants it. That's what keeps the screen working when a menu World creates a match and destroys itself.
Showing several Worlds at once is something only the editor's play mode does so far.
The editor's Worlds tab (beside History) edits project.json's bootstrap as a graph:
- Start-up Worlds and Spawnable Worlds are nodes; links are edges. + World, + Spawnable World and + Link create them, and each can be renamed or removed (links follow). Both kinds share one graph, laid out in columns from clients to the servers they link to; Spawnable Worlds are dashed and tagged "spawnable", as are links naming them, and each link's transport sits on a label beside its arrow.
- Selecting a Spawnable World shows the line of code that creates it,
Engine.createWorld("Name"), with a copy button, and the project scripts that mention its name in quotes. - Selecting one shows its tags (preset chips plus your own), tick rate, time scale, Renders, Input and start-up scenes.
- A checklist of every catalog system shows which ones the World runs and why: added by a tag, included or excluded by name. Ticking a box writes the smallest
include/excludethat gets you there. - Links have a from, a to and a transport; either end can be a start-up or a Spawnable World. Link
options(ports, URLs, conditions) are edited inproject.json(the Code panel); the panel keeps them. Worlds can also connect from scripts (world.connect(…)), which the panel can't see until you play. - While playing, the graph also draws the live connections between the session's Worlds, however they were made, with their state.
- Valid edits save at once, keeping the rest of
project.jsonand its formatting. Invalid ones stay unsaved, with the problems listed in the panel. The panel has its own undo and redo. - A project with a code bootstrap shows the script's path instead. Read-only projects (samples opened in a browser) show the panel disabled.
Play builds an Engine from the project's bootstrap, so you play the Worlds you configured:
- A start-up World whose config lists no
scenesplays the scene open in the editor."scenes": []starts empty, as it would in a game. - The game view tiles every World that renders, including Worlds created at runtime. Each tile is labelled with its World's name.
- Clicking a tile gives that World local input. Held keys stay held.
- A strip lists every World, headless ones too, with its state when it isn't playing, Pause / Resume buttons and Step, which advances just that World one frame, paused or not. These are a debugger's pause: they freeze the World without changing its
stateor callingonWorldPause. - Pause and Step in the menu bar (beside Play/Stop, in the middle) hold and advance the whole game: Step runs one 60 Hz frame in every World that isn't paused on its own, with a fixed step where one is due. Shortcuts: Ctrl+Shift+P pauses or resumes, F10 or Ctrl+Alt+P steps (Cmd on macOS).
- While playing, the Show picker at the top of the Hierarchy switches the Hierarchy and Inspector between the edited scene and any live World, Worlds created at runtime included. The live tree shows scenes, nodes made at runtime, ghosts and connections, and refreshes as the World changes. Selecting a node outlines it in that World's view and shows its live values in the Inspector. Edits there change the running game only.
- In a live World you can also change its structure: the Hierarchy's + and right-click menu create empty nodes (named in place), F2 or a double-click renames, Del deletes, and dragging a row onto another node reparents it (onto the World's name: back to the top level). The Inspector's Add Component and Remove work on the live node, and node fields pick from that World's nodes. These changes aren't undoable and are gone when you stop; Keep values only keeps field values.
- Keep values on a live component header (or Keep all changes to this node) writes those values into the edited scene when you stop, as one undoable step: plain values on scene nodes, overrides on prefab instances. Only nodes loaded from the scene you're editing can keep values; for others (made or instantiated at runtime, from another scene) the Inspector says why.
For client/server projects, the Network tab adds multiplayer play with several clients and simulated network conditions. See Multiplayer.
- Structural live edits (new, deleted or moved nodes, added or removed components) aren't kept on Stop; redo them in the scene.
- A bare
GameObject.instantiate(path)orTime.*after anawaitacts on the default World (above). - Plain field assignment across Worlds isn't caught.
- The Inspector has no per-instance
worldssetting yet; edit the scene file. - Outside the editor, only one World is visible at a time.
This wiki is generated from docs/wiki/ and the myco module's source on every push to master. Change pages there in a pull request: edits made here are not kept.
Basics
Gameplay
Rendering
Audio
- Playing sound
- Why is there no sound?
- Playlists
- Interactive music
- Sound generators
- Spatial audio
- Audio mixer
- Live mixing
- Loudness
- Audio profiler
- Audio takes
Reference