Skip to content

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.

  • 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 additive replaces 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.

When a scene is loaded, whether at start-up or by a script, MIRA:

  1. Loads its assets: models, textures, sounds. A script can follow the progress with an onProgress callback.
  2. Makes it active. By default it replaces the active scenes; with additive: true it joins them on top.
  3. Places the player at one of the scene’s spawn points, in play.
  4. 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 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-root is the top of a scene. It carries the scene-wide components: sky, background, fog, environment lighting. A script reaches it with scene.findEntity(scene.rootId).
  • ent-behavior holds 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 with self.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.

This walkthrough makes a button in one scene load another.

  1. Create two scenes in your project, named level-1 and level-2. Note their ids: Scenes.list() returns each scene’s id and name, and Scenes.load takes the id.

  2. In level-1, add an entity to click, such as a box, with a Selectable and a Signal Emitter whose signal is level/next.

  3. Add a Behavior entity to level-1 with 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 again
    log('Loading level-2');
    Scenes.load(nextId);
    });
    }
  4. Press Play and click the box. level-2 replaces level-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, in startup.
  • Scenes.load(id) replaces the active scenes. This script is in level-1, so it stops with its scene: its dispose hook would run, and nothing after the switch runs in it.
If you want… Use Read
To move to another level Scenes.load(id) Switching scenes
To show a HUD or menu over the current scene Scenes.load(id, { additive: true }) Switching scenes
To remove an added scene Scenes.unload(id) Switching scenes
To reach an entity in another loaded scene Scenes.find(nameOrId) Scenes
To keep the player or music across levels #pragma dont_unload() 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

Each scene entity type has a page of its components, props, events and a first script:

EntityTypeWhat it is
Behaviorent-behaviorHolds a user script and its run order.
Groupent-group
Scene Rootent-scene-rootSolid background color of the scene.
GlobalWhat it is
ScenesSwitch scenes: await Scenes.load(id), load(id, { additive: true }), unload(id), list(), active().
sceneThe 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.
SingletonNamed shared instances for this app: Singleton.get(key, factory), then Singleton.key.
selfThe entity the Behavior is on: self.position, self.getComponent(...). Its own Behavior is self.getComponent(BehaviorComponent).
findEntityFind an entity in the current scene by id, a map read fine every frame, or else by name among its entities.
spawnCopy an entity already in the scene (by name, or the entity itself) and add the copy with options.
destroySame as scene.destroy: remove an entity (or id) and its subtree, disposing them and stopping their scripts.
ComponentTypeWhat it does
Dont Unloadcomp-dont-unloadKeeps 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.
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.