Scene structure
Updated
A scene is one space of a world: a lobby, a level, a menu, a heads-up display. A project holds several scenes, and one or more of them are active, loaded and shown, at a time. This page explains what a scene is made of, how scenes stack when several are active, what happens when a scene loads, and how a script reads the scenes of its project. Changing the active scenes is covered in Switching scenes.
Before you start
Section titled “Before you start”- A scene is a hierarchy of entities. If entities and components are new to you, read Basic principles first.
- The examples are Behavior scripts. Write your first script shows how to add one and read its log.
What a scene is made of
Section titled “What a scene is made of”A scene is a saved tree of entities with a scene root at the top. Every other entity of the scene is under the root, directly or through its parents.
The scene root is an entity like the others, but it also carries the components that apply to the whole scene: the background, the sky, the fog, the environment lighting and the post-processing effects. Those are documented in the Lighting and Graphics sections.
When a scene is loaded, it becomes a live scene with its entities indexed by id. Inside a script, the scene the script’s own entity is in is the global scene:
| Member | What it is |
|---|---|
scene.name |
The scene’s name. |
scene.rootId |
The id of the scene root entity. |
scene. |
A Map of every entity in the scene, by id. |
scene. |
An entity of this scene by id only. |
scene. |
Any object of this scene by name: an entity, or a plain object. |
scene. |
Add an entity or a plain object at the top of the scene. |
scene. |
Remove an entity and its subtree, stop their scripts and free their memory. |
scene is live: when an entity marked Don’t Unload is carried into another scene, scene in its scripts refers to the new scene from then on. The global findEntity, spawn and destroy work in scene too. See Scene and entities.
Active scenes
Section titled “Active scenes”Several scenes can be active at once. Their order matters:
| Role | Which scene | What it decides |
|---|---|---|
| Base | The first active scene. | Usually the space the visitor is in, such as the current level. |
| Additive | Any scene loaded with additive: true, on top of the others. |
Its entities are added to the view, such as a HUD or an overlay. |
| Ruling | The scene activated last. | The scene-wide look of the whole view: its root’s background, environment lighting, fog and post-processing apply everywhere. |
When only one scene is active, it is base and ruling at once. Load a HUD additively and the HUD becomes the ruling scene: give the HUD’s root the look you want for the whole view, or leave its scene-wide settings matching the level.
How a scene loads
Section titled “How a scene loads”Loading a scene, at start-up or from a script, goes through four stages:
- Assets load. Models, textures, sounds and the other files its entities use. A script can follow the progress; see Switching scenes.
- It becomes active. It replaces the active scenes, or joins them on top when it is loaded additively.
- The player is placed. In play, the view moves to one of the scene’s spawn points. See Movement.
- It is shown. Loading resolves once the scene is drawn, so a loading screen held until then reveals it complete.
While any scene is still loading, physics does not step, so bodies are never sized from a model that has not arrived. A slow asset never holds a scene back for long: the scene shows, and that entity finishes loading on its own.
Every scene starts as saved. When a scene leaves the active set it is freed, and loading it again builds it fresh, without anything that changed while it was playing. Keep what must last across scenes yourself: see What survives a switch.
Reading scenes from a script
Section titled “Reading scenes from a script”The Scenes global lists the project’s scenes and finds loaded ones. These three methods work everywhere, in play and in editor code:
| Method | Returns | What it does |
|---|---|---|
Scenes.list() |
{ id, name }[] |
Every scene of the project, as a new array. |
Scenes. |
string[] |
The ids of the active scenes, base first, as a new array. The last one is the ruling scene. |
Scenes. |
a scene or undefined |
A loaded scene, by name or id; the newest if several share a name. undefined when it is not loaded. |
The scene Scenes.find returns is the same kind of object as scene, so findEntity(id) and getObjectByName(name) reach into it. Scenes.load and Scenes.unload, which change the active scenes, are on Switching scenes.
Examples
Section titled “Examples”1. Report the current scene
Section titled “1. Report the current scene”This script logs the scene it is in, its root and how many entities it holds.
#pragma lifecycle(startup)
function startup() { const root = scene.findEntity(scene.rootId); log('Scene:', scene.name); log('Root entity:', root ? root.name : 'none'); log('Entities:', scene.entities.size);}How it works:
sceneis the scene the Behavior entity is in.scene.rootIdis the root’s id, andscene.findEntitylooks an id up in this scene’s index.scene.entitiesis aMap, sosizeis the number of entities.
2. List the project’s scenes
Section titled “2. List the project’s scenes”This script lists every scene of the project and marks the active ones.
#pragma lifecycle(startup)
function startup() { const active = Scenes.active(); const ruling = active[active.length - 1]; Scenes.list().forEach((entry) => { let state = ''; if (entry.id === ruling) state = '(active, ruling)'; else if (active.includes(entry.id)) state = '(active)'; log(entry.name, entry.id, state); });}A three-scene project whose script logs every scene from Scenes.list() and marks the active, ruling one from Scenes.active().
Read the scene list in the script log.
How it works:
Scenes.list()returns{ id, name }for every scene, loaded or not.Scenes.active()returns the ids of the active scenes, base first, so its last entry is the ruling scene.- Both return new arrays each call: changing them changes nothing in the world.
3. Count what a scene is made of
Section titled “3. Count what a scene is made of”This script groups the scene’s entities by type, which is handy to check a scene’s content in play.
#pragma lifecycle(startup)
function startup() { const counts = new Map(); scene.entities.forEach((entity) => { counts.set(entity.entityType, (counts.get(entity.entityType) || 0) + 1); }); const sorted = Array.from(counts.entries()).sort((a, b) => b[1] - a[1]); sorted.forEach(([type, count]) => log(type, count));}How it works:
scene.entities.forEachvisits every entity of the scene, whatever its depth in the hierarchy.entityTypeis the entity’s type, such asent-primitive-meshorent-point-light.- The counts are sorted with the most common type first.
4. Reach into another loaded scene
Section titled “4. Reach into another loaded scene”A level script updates a label that lives in a HUD scene loaded on top. The HUD scene is named hud and holds a UI Label entity named Score.
#pragma lifecycle(startup)
let score = 0;
function findHudLabel() { const hud = Scenes.find('hud'); if (!hud) return undefined; const label = hud.getObjectByName('Score'); return isEntity(label) ? label.getComponent(UILabelComponent) : undefined;}
function startup() { on('coin/collected', () => { score += 10; const label = findHudLabel(); if (label) label.setProps({ text: 'Score: ' + score }); else log('The HUD is not loaded; score is', score); });}How it works:
Scenes.find('hud')returns the loaded HUD scene, orundefinedwhen it is not loaded.getObjectByNamefinds an object by name in that scene.isEntitychecks it is an entity before asking it for a component.- The label is looked up when a coin is collected rather than once in
startup, because the HUD may be loaded, unloaded and loaded again in between: each load builds new entities.
Common mistakes
Section titled “Common mistakes”Looking up a name with scene.findEntity
Section titled “Looking up a name with scene.findEntity”scene.findEntity takes an id only. The global findEntity takes an id or a name.
const door = scene.findEntity('Door'); // undefined: 'Door' is a name, not an idconst door = findEntity('Door');Assuming another scene is loaded
Section titled “Assuming another scene is loaded”Scenes.find returns undefined for a scene that is not loaded. Check before you use it:
const hud = Scenes.find('hud');hud.getObjectByName('Score'); // throws when the HUD is not loadedconst hud = Scenes.find('hud');if (hud) hud.getObjectByName('Score');Keeping entities from a scene that left
Section titled “Keeping entities from a scene that left”When a scene unloads, its entities are disposed. A variable that still points at one of them points at a dead object, and a scene loaded again has new entities. Look entities in another scene up again after it reloads, as example 4 does.
Expecting a scene to remember its state
Section titled “Expecting a scene to remember its state”A scene that is switched away and loaded again starts as saved: a door opened earlier is closed again, a collected coin is back. Keep progress in a Singleton or on a Don’t Unload entity. See Switching scenes.