Skip to content

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 startup and update, 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.

  • 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.

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.

This script spins its parent object at a speed you set in the editor, and logs a different message on phones:

Spinner.jx
#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 lifecycle tells the runtime which hooks to call: startup once, then update every frame.
  • #pragma slot adds a Speed slider to the Behavior in XR Creator and declares the speed variable that holds its value.
  • #if IS_MOBILE keeps one of the two #define lines, depending on the device. The other is dropped before the script is compiled, so it costs nothing.
  • delta is the time since the last frame, in seconds, so the object turns at the same speed at any frame rate.

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.

A Behavior runs in a contained sandbox. It can use:

  • The MIRA globals, ready to use without importing anything: scene and findEntity for the world, Input for the keyboard, mouse, touch and XR controllers, Tween for animation, Scenes for switching scenes, Network for multiplayer, HttpRequest for web requests, delay for timers, log for 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.