Editor
Updated
MIRA runs a world in one of two modes. In the editor, the world is being authored: the simulation is frozen so you can place and tune things without them moving away. In play, the world runs: scripts, physics, interaction and networking all start. The two modes never mix, so a visitor’s player carries no editor code and pays no cost for it. This page explains what runs in each mode, how scripts behave in each, and how to give a script tools that help you author.
Before you start
Section titled “Before you start”- Basic principles introduces scenes, entities and Behavior scripts.
- Lifecycle hooks explains
startupandupdate, which run in play only. - Slots explains the editor fields a script declares; editor tools are built on them.
The two modes
Section titled “The two modes”- Editor. The scene stays exactly as authored. Nothing ticks: no script hooks, no physics steps, no animations playing on their own. The editor draws its own helpers instead, such as gizmos and icons, and has its own selection. When you leave a scene in the editor, your edits are kept with it.
- Play. The scene runs from its saved state. Scripts start, physics steps, pointers and controllers interact with objects, and networked objects join their room. When a scene leaves play, it is freed, and it starts again as saved the next time it loads.
What runs where
Section titled “What runs where”| Feature | Editor | Play |
|---|---|---|
| Behavior scripts | Only editor code: #editor_only scripts and #editor_begin regions |
Yes, through their hooks |
| Physics | Not stepped | Stepped |
| Selection, grabbing and handles in the scene | No; the editor has its own selection | Yes |
| Enable on Signal | Ignored, so the saved state never changes | Yes |
| Spawn points | Do not place the view | Place the player when a scene loads |
| Don’t Unload | Nothing moves between scenes | Marked entities move to the scene that stays |
| Networking | Never connects | Joins the room when a networked object appears |
Scene switching from scripts (Scenes.load, Scenes.unload) |
Throws | Yes |
Notifications (Notification) |
Does nothing | Shows them |
| Leaving a scene | Edits are kept with the scene | The scene is freed and starts as saved next time |
| Gizmos, icons and helpers | Yes | Never built |
Some components behave differently by design. A Billboard turns only UI panel content in the editor, so the saved rotation never changes. A video never plays by itself while you author.
Scripts in each mode
Section titled “Scripts in each mode”A Behavior’s hooks (startup, update, enable, disable, dispose) run in play only. In the editor no hook runs, so a spinner stands still and a script’s startup waits until you press Play.
A script can still carry editor code: functions that run when someone presses one of its buttons or changes one of its fields in the editor. Editor code lives between #editor_begin and #editor_end, or fills a whole script that starts with #editor_only. It is compiled separately and is absent from the player: a visitor never downloads, checks or runs it.
| Play code | Editor code | |
|---|---|---|
| Runs | Through the lifecycle hooks | When a slot sends an event (a button pressed, a value changed), or needs validating |
| Every global | Yes | Yes |
self, scene |
The running scene | The scene being edited |
slot(ref), a live handle to a slot |
No | Yes |
Editor: the selection and the viewport camera |
Throws | Yes |
Scenes.load, Scenes.unload |
Yes | Throws |
Scenes.list, Scenes.active, Scenes.find |
Yes | Yes |
Editor code is covered in full in Editor scripts, and #editor_begin with the other directives in Directives.
The Editor global gives editor code the selection and the viewport camera: see Editor scripts.
Examples
Section titled “Examples”1. A play script with an editor button
Section titled “1. A play script with an editor button”The script spins its parent in play. In the editor, a Reset rotation button puts the parent back straight.
#pragma slot(ref=speed, type=Slider, min=0, max=10, default=2)#pragma slot(ref=reset, type=Button, label="Reset rotation", on_event=onReset)#pragma lifecycle(update)
function update(dt) { self.parent.rotation.y += speed * dt;}
#editor_beginfunction onReset() { self.parent.rotation.set(0, 0, 0); log('Rotation reset on', self.parent.name);}#editor_endPlay code spins a windmill in play; an editor region adds a Reset rotation button that runs only in XR Creator and never ships to the player.
Watch the sails turn in play. Download the project and press Reset rotation in XR Creator to run the editor code.
How it works:
updateis play code: it runs only after you press Play.- The
resetslot is a Button in the editor.on_event=onResetnames the editor function it calls. onResetsits in an editor region, so it exists only in the editor and never ships to the player. In editor code,selfis the Behavior entity in the scene being edited.
2. An editor tool that lines up the selection
Section titled “2. An editor tool that lines up the selection”A whole #editor_only script is a tool: it never runs in play. This one lines the selected entities up along x.
#editor_only#pragma slot(ref=spacing, type=Number, default=2, min=0.1, max=20)#pragma slot(ref=align, type=Button, label="Line up selection", on_event=onAlign)
function onAlign() { const selected = Editor.selection(); if (selected.length < 2) { log('Select at least two entities.'); return; } selected.forEach((entity, i) => entity.position.set(i * spacing, entity.position.y, 0));}An #editor_only tool script: select some crates in XR Creator and press Line up selection to set them in a row along x, Spacing apart.
Nothing moves in play: this script is a tool. Download the project, select the crates in XR Creator and press Line up selection.
How it works:
#editor_onlymust be the first directive: it makes every line editor code, and play skips the script without an error.Editor.selection()returns the selected entities in the order you selected them.spacingholds the slot’s saved value when the button is pressed. Changing the field changes the gap the next time you line up.
Common mistakes
Section titled “Common mistakes”Expecting update to run while editing
Section titled “Expecting update to run while editing”Hooks run in play only. A script that animates something shows nothing in the editor until you press Play. To change the scene while authoring, use an editor button, as example 1 does.
Calling play functions from editor code
Section titled “Calling play functions from editor code”Editor code sees only its own functions, the slot variables and the globals. A function declared in play code does not exist in the editor region.
function straighten() { self.parent.rotation.set(0, 0, 0);}
#editor_beginfunction onReset() { straighten(); // not defined in editor code}#editor_end#editor_beginfunction straighten() { self.parent.rotation.set(0, 0, 0);}function onReset() { straighten();}#editor_endSwitching scenes from editor code
Section titled “Switching scenes from editor code”Scenes.load and Scenes.unload throw in editor code, because they would change the scene being edited. Open the other scene in the editor instead, or switch scenes from play code.
Using Editor in play
Section titled “Using Editor in play”Every Editor member throws in play. Keep Editor calls inside #editor_begin regions or #editor_only scripts.
#pragma lifecycle(startup)
function startup() { log(Editor.selection().length); // throws in play}#editor_only#pragma slot(ref=count, type=Button, label="Count selection", on_event=onCount)
function onCount() { log(Editor.selection().length, 'selected');}Expecting Enable on Signal to react while editing
Section titled “Expecting Enable on Signal to react while editing”Signals are ignored by Enable on Signal in the editor, so the saved state of your scene never changes behind your back. Test signal wiring in play.