Behaviors
Updated
A Behavior is a small script that lives in the scene, attached to an entity, and gives the world its logic: a door that opens when a button is pressed, a coin that spins and disappears when collected, a score that counts up, a level that loads the next one. A few lines are enough, and the result runs the same in the editor’s Preview, for every visitor and on every device.
Behaviors are powered by the MIRA scripting runtime, the part of the MIRA runtime that runs scripts. You write JavaScript, and the runtime extends it:
- A preprocessor with its own directives. Lines that start with
#are read before the script runs. They declare what the script is (its hooks, its editor fields, what it shares) and can shape the code itself, for example keeping a block only on phones. - Hooks the runtime calls. A script names functions such as
startupandupdate, and the runtime calls them at set moments of the world’s life. - A contained sandbox with a ready-made toolbox. Each script gets the MIRA globals for the scene, input, animation, sound and multiplayer, plus a focused subset of standard JavaScript, and nothing else.
Behavior scripts are saved as .jx files.
Why Behaviors are worth knowing
Section titled “Why Behaviors are worth knowing”- They live in the scene. A Behavior is an entity: you place it, parent it under the object it drives and save it with the project. Copy the object and its logic comes along; download the project and the scripts are in it.
- They are fast. The runtime checks a script once, when it loads, then the browser runs it at full speed. How a script runs explains each step.
- They are contained. A script runs in its own sandbox. It sees only what the runtime gives it, so it behaves the same on every page and device, and everything it starts is cleaned up when it stops.
- They are tunable without code. A script can declare editor fields, such as a speed slider or a target object, so you or a teammate can adjust it in XR Creator without opening the code.
- They work together. Scripts talk through signals and shared values, without knowing about each other, so you can build a world from small scripts that each do one thing.
Behaviors in a scene
Section titled “Behaviors in a scene”In XR Creator you add a script as a Behavior entity. It holds one Behavior component
(comp-behavior), which stores the script’s code and
the values of its editor fields. self connects the script to the world: it is the entity the Behavior is on. Put the Behavior entity under the
object it should drive and reach that object with self.parent, or find any object by name with
findEntity('Door').
An entity holds one Behavior. For several scripts, use several Behavior entities.
A first look
Section titled “A first look”This script spins its parent object at a speed you set in the editor, and logs a different message on phones:
#pragma lifecycle(startup, update)#pragma slot(ref=speed, type=Slider, min=0, max=10, step=0.1, default=2)
#if IS_MOBILE#define GREETING "Spinning on a phone"#else#define GREETING "Spinning"#endif
function startup() { log(GREETING);}
function update(delta) { self.parent.rotation.y += speed * delta;}#pragma lifecycletells the runtime which hooks to call:startuponce, thenupdateevery frame.#pragma slotadds a Speed slider to the Behavior in XR Creator and declares thespeedvariable that holds its value.#if IS_MOBILEkeeps one of the two#definelines, depending on the device. The other is dropped before the script is compiled, so it costs nothing.deltais the time since the last frame, in seconds, so the object turns at the same speed at any frame rate.
How a script runs
Section titled “How a script runs”When the world starts, the runtime checks each script and runs startup once, then update every frame, for every
script in turn. When the script stops (its Behavior is removed, its entity destroyed or its scene unloaded),
dispose runs, and everything the script started (listeners, timers, tweens, requests) is released for you.
Scripts run only while the world plays: in Preview and for visitors, not while you edit.
The sandbox
Section titled “The sandbox”A Behavior runs in a contained sandbox. It can use:
- The MIRA globals, ready to use without importing anything:
sceneandfindEntityfor the world,Inputfor the keyboard, mouse, touch and XR controllers,Tweenfor animation,Scenesfor switching scenes,Networkfor multiplayer,HttpRequestfor web requests,delayfor timers,logfor the script log, and many more. - A subset of standard JavaScript:
Math,JSON,Date,Map,Set,Promise, typed arrays and the like. The language rules list what a script may use.
Everything else of the web page is out of reach: window, document, fetch, setTimeout, localStorage and
eval do not exist inside a script. The MIRA globals replace them with versions that fit the world: HttpRequest
instead of fetch, delay instead of setTimeout, shared values instead of page-wide variables. Because a script
only touches what the runtime gives it, it behaves the same in Preview, for visitors and on every device, and it can
never leave a timer or listener running after it stops.
The sandbox keeps scripts predictable, but it is not a security boundary: only run scripts from authors you trust.