Scenes
Updated
A world is a project: one or more scenes, the assets they use and the scripts that drive them. Splitting a world into scenes lets it load only what is needed: a menu, then a level, then the next level, with a heads-up display shown over each. This section explains what a scene is and the entities that give it its structure, how scenes are loaded and stacked, how a script switches between them with the Scenes global, and how to carry a player, music or a score from one scene to the next.
Key concepts
Section titled “Key concepts”- Project. Everything a world is made of: its scenes, its assets and its scripts. A project has at least one scene, and one of them is the default scene, the one a visitor starts in.
- Scene. A saved hierarchy of entities under a scene root. The scene root carries the scene-wide settings: background, sky, fog, environment lighting and post-processing.
- Scene tree. Every entity sits under the root, directly or through its parents, and a child follows its parent: it moves, turns and scales with it, and it is switched off when the parent is disabled, scripts included. Building the tree with groups therefore decides what can be moved, hidden and copied in one call.
- Active scenes. The scenes loaded and shown right now. There is always at least one; there can be several at once.
- Base scene. The first active scene, usually the level or space the visitor is in.
- Additive scene. A scene loaded on top of the active ones instead of replacing them, such as a HUD or a menu overlay.
- Ruling scene. The scene activated last. Its root decides the scene-wide look of the whole view: background, environment lighting, fog and post-processing.
- Switching. Loading a scene without
additivereplaces every active scene with it. The scenes that leave are freed; loading one again builds it fresh, as saved. - Don’t Unload. A mark on an entity that carries it, with everything under it and its running scripts, into the scene that stays when its own scene is switched away.
- Editor and play. Scenes are authored in the editor and run in play. Scripts switch scenes in play only.
How it works
Section titled “How it works”When a scene is loaded, whether at start-up or by a script, MIRA:
- Loads its assets: models, textures, sounds. A script can follow the progress with an
onProgresscallback. - Makes it active. By default it replaces the active scenes; with
additive: trueit joins them on top. - Places the player at one of the scene’s spawn points, in play.
- Shows it. The loading promise resolves once the scene is drawn, so a loading screen held until then reveals a complete scene.
Physics does not step while a scene is still loading, so bodies are never built around a model that has not arrived yet. A slow asset never holds a scene back for long: the scene shows and that entity finishes loading on its own.
When a scene leaves the active set, its entities are disposed. Every script in it stops: its dispose hook runs, and its listeners, timers and tweens are released. What a script needs to keep across scenes lives somewhere that survives the switch: an entity marked Don’t Unload, a Singleton, or persisted data.
The scene entities
Section titled “The scene entities”The scene entities give a scene its structure rather than its content: the root at the top that holds the scene-wide settings, the groups that organise everything under it, and the Behavior entities that hold the scripts that bring it to life.
ent-scene-rootis the top of a scene. It carries the scene-wide components: sky, background, fog, environment lighting. A script reaches it withscene.findEntity(scene.rootId).ent-behaviorholds one Behavior script. It has a transform, so you can place it and parent it like any other entity. A common pattern is to put a Behavior entity under the object it drives and reach that object withself.parent.
Scripts reach this structure through a few globals: scene.findEntity(scene.rootId) returns the root, findEntity(name) finds any entity, self is the script’s own Behavior entity and self.parent the object above it, and spawn and destroy copy and remove whole subtrees.
Your first scene switch
Section titled “Your first scene switch”This walkthrough makes a button in one scene load another.
-
Create two scenes in your project, named
level-1andlevel-2. Note their ids:Scenes.list()returns each scene’sidandname, andScenes.loadtakes the id. -
In
level-1, add an entity to click, such as a box, with a Selectable and a Signal Emitter whose signal islevel/next. -
Add a Behavior entity to
level-1with this script:NextLevel.jx #pragma lifecycle(startup)function findSceneId(name) {const entry = Scenes.list().find((s) => s.name === name);return entry ? entry.id : undefined;}function startup() {const nextId = findSceneId('level-2');if (!nextId) {log('No scene named level-2 in this project');return;}on('level/next', (event) => {if (event.eventType !== EventType.Selectable.Select) return;findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it againlog('Loading level-2');Scenes.load(nextId);});} -
Press Play and click the box.
level-2replaceslevel-1, and the player is placed at one of its spawn points.
Scenes.list()returns every scene of the project as{ id, name }. The script looks the id up by name once, instartup.Scenes.load(id)replaces the active scenes. This script is inlevel-1, so it stops with its scene: itsdisposehook would run, and nothing after the switch runs in it.
Your first scene switch: clicking a box in level-1 looks up the scene named level-2 with Scenes.list() and loads it with Scenes.load.
Click the box to load level-2. Drag to orbit the camera, scroll to zoom.
Choosing
Section titled “Choosing”| If you want… | Use | Read |
|---|---|---|
| To move to another level | Scenes. |
Switching scenes |
| To show a HUD or menu over the current scene | Scenes. |
Switching scenes |
| To remove an added scene | Scenes. |
Switching scenes |
| To reach an entity in another loaded scene | Scenes. |
Scenes |
| To keep the player or music across levels | #pragma dont_ or the Don’t Unload component |
Switching scenes |
| To keep game state, such as a score, across levels | Singleton |
Modules |
| To know what runs in the editor and what in play | The two modes | Editor |
| To set the background, sky, fog or environment lighting of the whole scene | The Scene Root | Environment and sky |
| To move, hide, copy or grab several objects together | A Group | Common components |
| To run a script | A Behavior entity | Behavior scripts |
Entity types
Section titled “Entity types”Each scene entity type has a page of its components, props, events and a first script:
| Entity | Type | What it is |
|---|---|---|
| Behavior | ent-behavior | Holds a user script and its run order. |
| Group | ent-group | |
| Scene Root | ent-scene-root | Solid background color of the scene. |
In scripts
Section titled “In scripts”| Global | What it is |
|---|---|
Scenes | Switch scenes: await Scenes.load(id), load(id, { additive: true }), unload(id), list(), active(). |
scene | The current scene fragment; live, so it always reflects the loaded scene. To delete an entity use scene.destroy(entity): it stops its scripts and frees its memory. scene.remove(entity) (and scene.clear()) stop the scripts but keep the memory, for an entity you will add back later. |
Singleton | Named shared instances for this app: Singleton.get(key, factory), then Singleton.key. |
self | The entity the Behavior is on: self.position, self.getComponent(...). Its own Behavior is self.getComponent(BehaviorComponent). |
findEntity | Find an entity in the current scene by id, a map read fine every frame, or else by name among its entities. |
spawn | Copy an entity already in the scene (by name, or the entity itself) and add the copy with options. |
destroy | Same as scene.destroy: remove an entity (or id) and its subtree, disposing them and stopping their scripts. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| Dont Unload | comp-dont-unload | Keeps this entity, and everything under it, when its scene is switched away in play. It moves to the new scene as a child of its root. |
In this section
Section titled “In this section”| Topic | Description |
|---|---|
| Scenes | What a scene is, the scene root, base, additive and ruling scenes, and reading scenes from a script. |
| Switching scenes | The Scenes global, leaving your own scene, Don’t Unload, and what survives a switch. |