Project Mammut Wiki
projectmammut.com
Project Mammut is currently in open beta. The UI is being updated a lot during this time, and you may run into bugs. If you'd like to help develop the engine or suggest features, come find us on Bluesky or Discord.

Sub Scenes

A sub-scene is a normal scene file marked so it can be embedded inside another scene as a Scene node, rather than only ever being loaded full-screen. Common uses are a settings panel, a reusable UI overlay, or a character built from several layered parts. Mark a scene as one from its own root inspector in the Scene Editor; doing so is what makes it show up in a Scene node's asset picker at all.

A sub-scene has no background, music or ambient sound of its own, so those settings are hidden in its root inspector. The scene it's placed in sets them instead. Any that were set before the scene was marked as a sub-scene are ignored, both in the editor and in the game.

How it mounts

A Scene node compiles to a placeholder element sized and positioned like any other node. At runtime, the referenced sub-scene is loaded and rendered independently, then its rendered children are moved directly into that placeholder, the sub-scene's own root wrapper is discarded, and the placeholder becomes its effective parent. See Scenes for the exact onEnter/onStart ordering this produces for nested instances.

Each copy has its own script

Every Scene node gets its own copy of the sub-scene, with its own script, this.vars and this.dom. Placing the same sub-scene three times gives three independent copies. The scene they're placed in reaches a copy through its Scene node:

this.dom.coin_1.scene.collect();

Inside the copy, this.parentScene is the scene it's placed in. A copy starts fresh each time the scene containing it is shown. See Scene Script for more on reaching other scenes.

Size and Origin

Every sub-scene has its own Size and Origin, set in its root inspector under the Sub Scene toggle. While you edit a sub-scene, the canvas is that size, measured from the sub-scene's (0, 0) at its top-left corner.

  • Size is in pixels. Drag the handles on the right edge, bottom edge or bottom-right corner of the canvas to change it, and hold Shift on the corner to keep its proportions. Fit to content sets it to the furthest point any node reaches from (0, 0).
  • Origin is the point the sub-scene rotates and scales around, as a percentage of its size: 50% 50% is the centre and 50% 100% is the middle of the bottom edge, such as a character's feet. Drag the round handle on the canvas to move it, and hold Shift to snap to quarters.
  • Nodes sized or placed in % inside the sub-scene are measured against its Size, both on the canvas and in the game.

Every Scene node showing the sub-scene takes this Size and Origin, and follows any change to them. Its X and Y still place its top-left corner.

A sub-scene made before Size existed starts at the size of its content, and keeps it once the sub-scene is next saved. Convert to Scene gives the new sub-scene the size and origin of the node it was made from.

A copy added with SceneManager.AttachScene to a container with a layout also takes the Size and Origin, so it's laid out like any other item in that container. Attached anywhere else, it fills the element it's attached to.

Scene Variables versus the Variable Store

Scene Variables are a separate mechanism from the project-wide Variable Store. A scene declares its own Scene Variables in its root inspector, using the same editor as the Variables Store: a name, a type (String, Number, Boolean, File, Array or Object) and a default value. Each instance embedding that scene can independently override any of them without touching the sub-scene file itself.

In an instance's inspector you can change the values, including array items and object fields, but the types and fields come from the sub-scene. Reset puts a variable back to the sub-scene's default. If the sub-scene later changes a variable's type or fields, an existing override is marked as no longer matching, and it keeps being used until you reset it.

Overrides are applied to the sub-scene's this.vars before it mounts, so onEnter() and onStart() already see the overridden values. Unlike Store variables, Scene Variables aren't global and aren't included in a save file, they only exist as long as that particular scene instance is mounted, scoped to it and it alone. See Scene Node for the override fields themselves, and Coding for this.vars.

Cycle prevention

Picking a sub-scene that would embed itself, either directly or through a chain of other sub-scenes, is blocked with an error rather than silently allowed or left to fail at runtime.

Extracting an existing scene

Most sub-scenes start life the other way around: select a subtree of nodes in a normal scene and use Convert to Scene from the right-click menu. That extracts the selection into its own new sub-scene file, sized to match, and replaces the original nodes in place with a Scene node pointing at it, the on-screen result looks identical immediately after converting.