Skip to content

Behavior

Updated

Holds a user script and its run order.

Type
ent-behavior
Script class
BehaviorEntity
Family
Scene

A Behavior entity holds one script. Its required Behavior component (comp-behavior) is data only: it stores the script’s source, its run order and the values of its editor fields, and the scripting runtime reads it and runs the code.

The script runs in play. In the editor the scene stays still, so no startup or update runs; only editor code (between #editor_begin and #editor_end, or in an #editor_only script) reacts to the script’s buttons and fields there. A script runs while its Behavior, its entity and every ancestor entity are enabled.

Behavior entities can be placed and parented like any other entity. The common pattern is to put one under the object it drives and reach that object with self.parent. Inside the script, self is the Behavior entity, and self.getComponent(BehaviorComponent) its Behavior; this is not the entity, so always write self. An entity holds one Behavior: use several Behavior entities for several scripts.

The props that matter most:

  • code is the script source. Changing it restarts the script.
  • executionIndex is the run order against the other Behaviors, lowest first. Negative values are allowed.
  • slotValues holds the values of the script’s slots (editor fields), saved by the editor.
  1. Add a Behavior entity to the scene (see Scenes and hierarchy), usually under the object it should drive.
  2. Open its code. A new one starts with empty startup, update(delta, time) and dispose hooks, declared by #pragma lifecycle(startup,update,dispose).
  3. Write the script, and declare #pragma slot fields for values a designer should tune without editing code.
  4. Set executionIndex only when the order between scripts matters, for example when one script must read what another wrote in the same frame.
  5. Press play to run it.

Goal: the object above the Behavior entity turns at a speed set in the editor.

Spin.jx
#pragma slot(ref=speed, type=Slider, min=0, max=10, step=0.1, default=2)
#pragma lifecycle(startup, update)
let target = null;
function startup() {
target = self.parent;
log('Spinning', target.name);
}
function update(dt) {
target.rotation.y += speed * dt;
}
  • #pragma slot(ref=speed, …) declares the speed variable and shows it as a slider in the editor. In play it holds the saved value, or the default.
  • #pragma lifecycle(startup, update) names the hooks the engine calls: startup once, update every frame with the frame time dt in seconds.
  • self.parent is the object the Behavior entity sits under.

Goal: a ticking counter that stops while its Behavior entity is disabled, and says so.

Ticker.jx
#pragma lifecycle(startup, update, enable, disable, dispose)
let seconds = 0;
function startup() {
log('Ticker started');
}
function update(dt) {
seconds += dt;
}
function enable(source) {
log('Resumed by', source.name);
}
function disable(source) {
log('Paused by', source.name, 'after', seconds.toFixed(1), 's');
}
function dispose() {
log('Ticker stopped for good');
}
  • When the Behavior, its entity or an ancestor is switched off, disable(source) runs and the script pauses: no update until it is switched on again, when enable(source) runs.
  • source is what was switched: an entity, or the Behavior component itself.
  • dispose runs only when the script stops for good, for example when its entity is destroyed or its scene unloads, not on disable.

Goal: one script switches a second Behavior, the Alarm script, on and off when the player presses A.

The Behavior entity named Alarm holds the alarm script.

AlarmSwitch.jx
#pragma lifecycle(startup, update)
let alarm;
function startup() {
alarm = findEntity('Alarm').getComponent(BehaviorComponent);
log('Alarm runs at index', alarm.props.executionIndex);
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.A)) return;
alarm.setEnabled(!alarm.enabled);
log(alarm.enabled ? 'Alarm on' : 'Alarm off');
}
  • getComponent(BehaviorComponent) returns the Behavior on the Alarm entity; props reads its props, such as executionIndex.
  • setEnabled on the component pauses or resumes that script only; the alarm script receives disable or enable with the component as source.
  • Input.keyboard.isKeyReleased(Key.A) is true for one frame after the key goes up.
  • Find entities by name once, in startup, and keep them: a lookup by name walks the scene.
  • executionIndex orders the frame’s hooks, not loading. A script that imports another must load after it.
  • startup may be async to await something, but the engine does not wait for it: update already runs meanwhile.

What a new Behavior is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Behavior comp-behavior Required One Holds a user script and its run order.

You can also add 5 optional components, such as Animation Graph, Animation Timeline, Dont Unload, Motion Path Controller, Signal Emitter.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
code "/*\n* A directive that allows connection to lifecycle ho…
executionIndex 0 -9007199254740991 ≤ x ≤ 9007199254740991
slotValues {}
slotProps {}

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Behavior.

BehaviorEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Behavior');
if (!(entity instanceof BehaviorEntity)) return;
// Its Behavior component holds the settings listed under Props.
const component = entity.getComponent(BehaviorComponent);
log('executionIndex is', component.props.executionIndex);
component.setProps({ executionIndex: 1 });
}