Inside a scene script, this is the copy of the scene that's showing. It holds the lifecycle hooks you write, the scene's elements and variables, and any values and methods you add yourself. See Scene Script for how a script is written.
A fresh copy is made each time a scene is shown, attached or placed in a Scene node, so every copy starts with its variables and members at their starting values.
| Member | Description |
|---|---|
onEnter() |
Runs when the copy has been built, before its sub-scenes start |
onStart() |
Runs once every sub-scene inside has started |
onExit() |
Runs when the copy is removed |
root |
The scene's root element |
dom |
The scene's elements, by node id |
vars |
The scene's variables |
attachedScenes |
The sub-scene copies placed in this scene |
parentScene |
The scene this copy is placed in |
dom.<id>.scene |
The copy drawn into a Scene node |
Lifecycle hooks
All three hooks are optional. For a menu scene with a sub-scene card inside it, they run in this order:
menuonEnter()cardonEnter()cardonStart()menuonStart()
When menu is removed, its onExit() runs, then card's.
onEnter
onEnter(): void | Promise<void>
Runs when the copy has been built and added to the page, before any sub-scenes inside it start.
How it works
this.dom,this.rootandthis.varsare ready.- The music and ambient sound set in the scene's inspector have already started.
- It can be
async, but nothing waits for it.onStart()runs as soon asonEnter()reaches its firstawait.
Example
onEnter() {
this.dom.title.textContent = this.vars.title; // fill in text before anything else runs
this.dom.results_panel.hide(); // start with the panel hidden
},
onStart
onStart(): void | Promise<void>
Runs after onEnter(), once every sub-scene inside this one has run its own onEnter() and onStart(). Most setup goes here: registering handlers, starting chapters and talking to sub-scenes.
How it works
- It can be
async, but nothing waits for it. - A sub-scene's
onStart()runs before its parent's, so the parent can rely on its sub-scenes being ready.
Example
async onStart() {
InputManager.Register("KeyDown", "Escape", this.openPauseMenu, this); // removed when the scene exits
Revealer.Init(this.dom.text_box);
await Revealer.StartRevealChapterByID("chapter_01");
},
onExit
onExit(): void
Runs when the copy is removed: the game switched to another scene, the scene was detached with SceneManager.DetachScene, or the scene containing it was removed.
How it works
- The scene's elements are still on the page while it runs.
- Handlers registered with
thisas the owner are removed straight after it. - The
onExit()of each sub-scene inside runs after this one. - Timers, intervals and listeners on elements outside the scene keep running unless you stop them here.
Example
timer: 0,
onStart() {
this.timer = setInterval(() => this.tick(), 1000); // calls tick() every second
},
onExit() {
clearInterval(this.timer); // otherwise tick() keeps running after the scene has gone
},
Members
root
this.root: MammutElement<HTMLDivElement>
The scene's root element, the same as this.dom.root. It has all the Element Helpers.
How it works
- For a full-screen scene, it's the element that fills the screen.
- For a sub-scene in a Scene node, it's that Scene node's element in the parent scene, so hiding it hides the whole sub-scene.
- It's ready from
onEnter().
Example
closePanel() {
this.root.hide(); // in a sub-scene, hides the Scene node it's drawn into
},
dom
this.dom: { root: MammutElement; [nodeId: string]: MammutElement }
The scene's elements, by node id. It holds root, plus every node with Referenceable ticked in the inspector. Scene, animator, particles, 3D Scene and Spine nodes are always included.
How it works
- Each entry is the real HTML element, so the usual browser properties work, such as
textContent,styleandsrc. - Each entry also has the Element Helpers, and node types like animators add their own controls.
- The type of each entry depends on its node. See Script Types.
- It's ready from
onEnter().
Example
onStart() {
this.dom.score_label.textContent = `Score: ${Store.Get("score", 0)}`;
this.dom.portrait.src = Assets.GetImage("alice_happy")?.url ?? ""; // an image node is an <img>
this.dom.next_button.addEventListener("click", () => this.next());
},
vars
this.vars: { [name: string]: any }
The scene's variables, from the Scene Variables list in its inspector. Each copy has its own set.
How it works
- They start at the values set in the inspector each time the scene is shown.
- A Scene node can give its copy different starting values. See Scene Node.
- A File variable is an AssetRef, which you can pass straight to the engine function for its kind, such as
SceneManager.AttachScene(this.vars.menu, this.dom.overlay). - For values that last after the scene has gone, use Store. See Variable Scope.
Example
onHit() {
this.vars.health -= 10;
if (this.vars.health <= 0) SceneManager.SwitchScene("game_over");
},
attachedScenes
this.attachedScenes: SceneObject[]
The sub-scene copies placed in this scene with Scene nodes, in the order they were added.
How it works
- Scenes added with
SceneManager.AttachScenearen't included. - Each entry is the same object as
this.dom.<id>.scenefor its Scene node.
Example
resetCards() {
for (const card of this.attachedScenes) {
card.reset?.(); // calls reset() on every sub-scene that has one
}
},
parentScene
this.parentScene: SceneObject | null
Inside a sub-scene placed with a Scene node, the copy of the scene it's placed in. Inside a scene added with SceneManager.AttachScene, the scene whose element it was attached to.
How it works
- It's
nullfor a full-screen scene, and for a scene attached to an element that isn't inside any scene. - It's typed loosely, so the parent's own methods don't autocomplete. Check a method exists before calling it.
Example
onClosePressed() {
this.parentScene?.onCardClosed?.(this); // tell the scene this card is in that it was closed
},
dom.<id>.scene
this.dom.<sceneNodeId>.scene
On a Scene node, the copy of the sub-scene drawn into it. Each Scene node has its own copy, with its own script members and variables.
How it works
- It's ready from
onEnter(). - It's typed from the sub-scene's own script, so its methods,
varsanddomautocomplete, and a misspelled method is an error. - To reach a scene that isn't inside this one, use
SceneManager.GetScene. See SceneManager.
Example
onStart() {
const left = this.dom.coin_1.scene;
const right = this.dom.coin_2.scene;
left.collect(1); // only changes the left coin
right.vars.value = 50; // each copy has its own vars
},