Behavior
Updated
Holds a user script and its run order.
- Type
ent-behavior- Script class
BehaviorEntity- Family
- Scene
Overview
Section titled “Overview”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:
codeis the script source. Changing it restarts the script.executionIndexis the run order against the other Behaviors, lowest first. Negative values are allowed.slotValuesholds the values of the script’s slots (editor fields), saved by the editor.
Set it up
Section titled “Set it up”- Add a Behavior entity to the scene (see Scenes and hierarchy), usually under the object it should drive.
- Open its code. A new one starts with empty
startup,update(delta, time)anddisposehooks, declared by#pragma lifecycle(startup,update,dispose). - Write the script, and declare
#pragma slotfields for values a designer should tune without editing code. - Set
executionIndexonly when the order between scripts matters, for example when one script must read what another wrote in the same frame. - Press play to run it.
Examples
Section titled “Examples”Spin the parent
Section titled “Spin the parent”Goal: the object above the Behavior entity turns at a speed set in the editor.
#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 thespeedvariable 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:startuponce,updateevery frame with the frame timedtin seconds.self.parentis the object the Behavior entity sits under.
Pause and resume with the entity
Section titled “Pause and resume with the entity”Goal: a ticking counter that stops while its Behavior entity is disabled, and says so.
#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: noupdateuntil it is switched on again, whenenable(source)runs. sourceis what was switched: an entity, or the Behavior component itself.disposeruns only when the script stops for good, for example when its entity is destroyed or its scene unloads, not on disable.
Turn another script on and off
Section titled “Turn another script on and off”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.
#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 theAlarmentity;propsreads its props, such asexecutionIndex.setEnabledon the component pauses or resumes that script only; the alarm script receivesdisableorenablewith the component assource.Input.keyboard.isKeyReleased(Key.A)istruefor one frame after the key goes up.
- Find entities by name once, in
startup, and keep them: a lookup by name walks the scene. executionIndexorders the frame’s hooks, not loading. A script that imports another must load after it.startupmay beasynctoawaitsomething, but the engine does not wait for it:updatealready runs meanwhile.
Components
Section titled “Components”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 |
"/ |
|
execution |
0 |
- |
slotValues |
{} |
|
slotProps |
{} |
Events
Section titled “Events”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.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
OpenCV.js, imported from a CDN, finds the edges in a picture drawn on a script canvas with Canny edge detection, and two planes show the picture and its edges side by side.
OpenCV.js is an 11 MB download (2.8 MB compressed): wait a few seconds for the edges to appear. Then click the scene and press Space to try other thresholds. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”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.
#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 });}