Skip to content

Events

Events trigger scripts. Scenery has two kinds: object events that fire for a specific object, and scene events that fire globally.

Object events

Attach to individual objects. The triggering object is available in scriptContext.sourceEvent.

EventFires when
On Will AppearObject is about to be added to scene
On Did AppearObject has appeared
On RemovalObject is removed from scene
On Tap GestureObject is tapped
On Camera CollisionCamera enters/exits object bounds
On Object CollisionPhysics collision with another object
Distance FieldCamera or target crosses distance threshold
Look DirectionUser looks at/away from object (or object looks at target)
Media PlaybackVideo/audio begins, reaches timestamp, or ends
javascript
// On Tap Gesture – scale up the tapped object
scriptContext.sourceEvent.objectEntity.animateTo(
    { scale: Vector3(1.2, 1.2, 1.2) },
    0.2
);

Scene events

Fire regardless of which object is involved.

EventFires when
On Experience StartExperience begins
On Experience LoadExperience finishes loading
On Screen TapUser taps anywhere on screen
On RenderEvery frame
On ScheduleAfter a delay or at specific time
On Variable ChangeA variable is modified
On Audio AnalysisAudio levels update (ambient or microphone)
javascript
// On Render – rotate every frame
var cube = scene.findEntity("YOUR_OBJECT_ID");
cube.representation.rotation = cube.representation.rotation.multiply(
    Rotation(0, scriptContext.sourceEvent.deltaTime, 0)
);

Subscribing in code

You can also subscribe to events programmatically:

javascript
var cube = scene.findEntity("YOUR_OBJECT_ID");

// Object event
cube.on('tap', function(e) {
    e.objectEntity.animateTo({ scale: Vector3(1.2, 1.2, 1.2) }, 0.2);
});

// Scene event
scene.on('render', function(e) {
    cube.representation.rotation = cube.representation.rotation.multiply(
        Rotation(0, e.deltaTime, 0)
    );
});

// One-time listener
scene.once('start', function() {
    console.log('Experience started');
});

Throttling

For expensive operations, throttle the render loop:

javascript
scene.on('render', { throttle: 0.1 }, function(e) {
    // Runs every 100ms instead of every frame
});

Unsubscribing

javascript
var eventId = cube.on('tap', function() {});
// Later...
cube.off(eventId);

objectEntity vs representationEntity

Tap and collision events give you both objectEntity (the top-level container) and representationEntity (the specific visual that was interacted with). Use objectEntity when you need to find sibling representations like audio sources.

Event data

How you access event data depends on how your script runs:

Run Script action – use scriptContext.sourceEvent:

javascript
var entity = scriptContext.sourceEvent.objectEntity;
var dt = scriptContext.sourceEvent.deltaTime;

Event subscriptions – data is passed to your callback:

javascript
scene.on('render', function(e) {
    var dt = e.deltaTime;
});

cube.on('tap', function(e) {
    var tapped = e.objectEntity;
});

Properties by event type:

EventProperties
renderdeltaTime, time
tapobjectEntity, representationEntity, screenPosition
screenTapposition
distanceobjectEntity, distance, crossed
lookAtobjectEntity, isLooking
cameraCollisionobjectEntity, representationEntity
physicsCollisionobjectEntity, representationEntity, position, impulse
variableChangevariableId, value
mediaPlaybackobjectEntity, status, time, duration
audioAnalysiskind, loudness, bass, lowMid, mid, upperMid, presence, brilliance, air, treble
add, appear, removeobjectEntity, representationEntity

TIP

Use console.log(scriptContext.sourceEvent) or console.log(e) to inspect all available properties.