Skip to content

Selection and grabbing

Updated

Three components make an entity respond to the player’s pointer without any code. Selectable lets the player click or touch it to select it. Grabbable lets them pick it up and carry it. Pivotable puts a gizmo of arrows, rings and spheres on it, so they can move, turn and scale it precisely. Each one works the same with a mouse, a finger, an XR controller’s ray or a tracked hand, and each tells your scripts what happened through events.

You should know:

  • How events reach a script through a Signal Emitter and on(signal, callback); see Events and signals.
  • How to change a component’s props from a script with setProps; see Components.
  • For grabbed objects that fall and bounce, what a rigid body is; see Physics.
Component Type What the player does
Selectable comp-selectable Clicks or touches the object to select it. One object is selected at a time.
Grabbable comp-grabbable Picks the object up and carries it.
Pivotable comp-pivotable Moves, turns or scales the object with a gizmo.

To drag an object within limits (a door, a drawer, a knob), use a Handle instead.

All three run in play only. In the editor, the editor’s own selection and transform gizmo take over, and these components listen to nothing. They are event-driven: an idle one does no work per frame.

A pointer is whatever the player points with. Interaction components tell two kinds apart:

Kind pointerType values Where
cursor screen-mouse, screen-touch, screen-pen, ray A mouse, a finger or a pen on the screen; an XR controller’s ray.
hand grab, touch An XR hand close to the object: a pinch or grab (grab), or a fingertip poke (touch).

Selectable and Grabbable have a cursor prop and a hand prop. Set one to false and that kind of pointer passes through the object as if the component were not there. Pivotable takes every pointer.

A press on any mesh under the entity, including a child entity’s mesh, counts as a press on the entity. When a Grabbable child sits inside a Grabbable group, the nearest Grabbable takes the press, so the child is grabbed on its own.

Selectable, Grabbable, Pivotable and Handle all send two pointer events, for pointers they accept:

eventType data When
<Source>::hover { pointerType } A pointer came over the entity or any of its children.
<Source>::leave { pointerType } That pointer left it.

<Source> is the component that sends it: Selectable, Grabbable, Pivotable or Handle. In a script, compare with EventType.Selectable.Hover, EventType.Grabbable.Leave and so on. Like every component event, they reach scripts only through a Signal Emitter on the entity.

Selectable tracks whether its entity is selected.

  • A click or touch on the entity selects it.
  • Selection is single across the scene: selecting one entity deselects every other Selectable.
  • A click on empty space clears the selection.
  • A script can select it too, with setSelected(true) or by setting the selected prop.
  • A Pivotable with activation: 'selection' shows its gizmo only while the entity is selected.
Prop Type Default Description
selected boolean false Selected state applied when the entity becomes active. Changing it at runtime selects or deselects.
cursor boolean true Selectable with a screen pointer or an XR controller ray.
hand boolean true Selectable with an XR hand, by a grab or a touch.
eventType EventType constant data When
Selectable::select EventType.Selectable.Select { pointerType? } It was selected. pointerType is absent when a script selected it.
Selectable::deselect EventType.Selectable.Deselect { pointerType? } It was deselected.
Selectable::change EventType.Selectable.Change { selected, pointerType? } Right after select or deselect, with the new state.
Selectable::hover, Selectable::leave EventType.Selectable.Hover, .Leave { pointerType } See Shared pointer events.
Member Description
selected true while the entity is the current selection.
setSelected(selected) Select or deselect it from a script. Sends select or deselect, then change.
SelectableComponent.isSelected(entity) Whether entity has a Selectable that is selected now.

Grabbable lets the player pick the entity up and carry it. How the entity follows depends on the pointer that grabbed it:

Pointer How the entity follows
Screen pointer (screen-*) It is held in the camera’s frame: walking or turning carries it along, and moving the pointer drags it at the depth where it was grabbed.
XR controller ray (ray) It is attached to the controller at the ray’s distance, following every move and turn.
XR hand (grab, touch) It is attached to the hand, following every move and turn.

While a grab is live:

  • the grab is exclusive, and the pointer is captured, so a fast drag is not lost;
  • the camera controller stands down, so the drag does not also turn the camera;
  • a simulated rigid body on the entity is driven kinematically: it stops falling and pushes other bodies. It gets its own type back on release, so a dynamic crate falls when dropped.

A grab ends when the pointer is released or cancelled, and also when the component stops listening: the entity is deactivated, or a prop change turns its pointer off.

Prop Type Default Description
cursor boolean true Grabbable with a screen pointer or an XR controller ray.
hand boolean true Grabbable with an XR hand close to it.

With both off, the entity is not hit by pointers at all.

eventType EventType constant data When
Grabbable::start EventType.Grabbable.Start { pointerType } A grab began.
Grabbable::end EventType.Grabbable.End { pointerType } The grab ended, however it ended.
Grabbable::hover, Grabbable::leave EventType.Grabbable.Hover, .Leave { pointerType } See Shared pointer events.

There is no event on every frame while the entity moves: read its transform if you need to follow it.

Member Description
isGrabbed true while the entity is held.

Pivotable puts an interactive gizmo on its entity: translate arrows, plane sliders, rotate rings, scale spheres and a free-move center. The player drags a part of the gizmo to move, turn or scale the entity.

Like Grabbable, a drag captures the pointer, makes the camera controller stand down and drives a simulated rigid body kinematically. An entity without a body is moved through its transform.

Prop Type Default Description
translate boolean true Show the translate arrows and plane sliders.
rotate boolean true Show the rotate rings.
scale boolean false Show the scale spheres.
scaleAxes { x?, y?, z? } of booleans all axes A false axis hides that axis’s scale sphere and keeps that axis’s scale unchanged. Missing axes are allowed.
space 'local' or 'world' 'world' Axis frame: local follows the entity’s orientation, world stays aligned with the world.
size number 1 Screen-size multiplier for the gizmo, 0.2 to 10.
activation 'selection' or 'always' 'selection' selection shows the gizmo while the entity’s Selectable is selected; always keeps it visible.
plane 'XY', 'YZ' or 'XZ' none Restrict the gizmo to one plane: its two arrows and scale spheres, its slider and the ring around its normal. Leave it out for full 3D.
interactive boolean true With false, the gizmo is a passive orientation indicator: only the axis arrows show, and nothing responds to the pointer.

With activation: 'selection', add a Selectable to the same entity, or the gizmo never shows.

eventType EventType constant data When
Pivotable::start EventType.Pivotable.Start { handle } A drag began. handle is arrow, slider, rotator, sphere or center.
Pivotable::end EventType.Pivotable.End { handle } The drag ended.
Pivotable::hover, Pivotable::leave EventType.Pivotable.Hover, .Leave { pointerType } See Shared pointer events.
Member Description
isDragging true while a part of the gizmo is being dragged.
  • Which entities. Grabbable, Selectable and Pivotable are offered on meshes, primitives, splats, text, media surfaces, UI panels, fire and groups; Selectable also on terrain and water. A grabbed group moves with everything under it.
  • Object manipulation setting. When the project’s Allow Object Manipulation player setting is off, Grabbable, Pivotable and Handle move nothing, and a live drag is released. Selectable keeps working.
  • Multiplayer. On a networked entity, selecting it, grabbing it or dragging its gizmo asks for its ownership, so what the player does reaches everyone. An entity locked to another player can still be selected, but not moved. See Multiplayer.
LampSwitch.jx
#pragma lifecycle(startup)
// The Lamp entity has a Selectable and a Signal Emitter with the signal 'lamp'.
// Its child Lamp Bulb holds the light.
function startup() {
const bulb = findEntity('Lamp Bulb');
const lamp = findEntity('Lamp').getComponent(SelectableComponent);
on('lamp', (event) => {
if (event.eventType !== EventType.Selectable.Change) return;
bulb.setEnabled(event.data.selected);
log(event.data.pointerType ? `Switched with ${event.data.pointerType}` : 'Switched by a script');
});
lamp.setSelected(true); // start with the lamp on
}
  • Selectable::change arrives after every select and deselect, with data.selected, so one check covers both.
  • setEnabled switches the bulb entity, and the light it holds, on or off.
  • pointerType is absent when a script selected the lamp, which is what happens on the last line: setSelected(true) sends select and change like a click would.
  • Because selection is single, clicking another Selectable turns the lamp off. To keep several things on independently, track your own state on Selectable::select instead.
HoverHighlight.jx
#pragma lifecycle(startup)
// Every exhibit has a Selectable, an Outline (disabled in the editor),
// and a Signal Emitter with the signal 'exhibits'.
function startup() {
on('exhibits', (event) => {
const entity = findEntity(event.entityId);
const outline = entity && entity.getComponent(OutlineComponent);
if (!outline) return;
if (event.eventType === EventType.Selectable.Hover) outline.setEnabled(true);
if (event.eventType === EventType.Selectable.Leave && !entity.getComponent(SelectableComponent).selected) {
outline.setEnabled(false);
}
if (event.eventType === EventType.Selectable.Deselect) outline.setEnabled(false);
});
}
  • All exhibits share one signal, so one listener serves them all. event.entityId is the exhibit that sent the event, and findEntity turns it into the entity.
  • The Outline component draws a coloured edge around the entity while it is enabled; see Graphics.
  • Hover turns the outline on. Leave turns it off, unless the exhibit is selected, so the selected exhibit stays highlighted. Deselect then removes it.

3. Crates that return when dropped off the edge

Section titled “3. Crates that return when dropped off the edge”
CrateReturn.jx
#pragma lifecycle(startup, update)
const FLOOR_LIMIT = -5; // below this height, the crate has fallen off
const crates = [];
function startup() {
for (const name of ['Crate A', 'Crate B', 'Crate C']) {
const entity = findEntity(name);
crates.push({ entity, grab: entity.getComponent(GrabbableComponent), home: entity.position.clone() });
}
on('crates', (event) => {
if (event.eventType === EventType.Grabbable.Start) log(`Picked up with ${event.data.pointerType}`);
if (event.eventType === EventType.Grabbable.End) emit('sfx/drop', { id: event.entityId });
});
}
function update() {
for (const crate of crates) {
if (crate.grab.isGrabbed || crate.entity.position.y > FLOOR_LIMIT) continue;
crate.entity.setPosition(crate.home.x, crate.home.y, crate.home.z);
log(`${crate.entity.name} returned`);
}
}
  • startup records each crate’s starting position with position.clone(), a copy that will not change as the crate moves.
  • While a crate is held, its dynamic body is driven kinematically; on release it falls again under gravity.
  • update checks each crate’s height. isGrabbed skips a crate the player is holding.
  • setPosition moves the crate and its rigid body. Writing position directly would move only what is drawn, and the body would pull it back.
GizmoModes.jx
#pragma lifecycle(startup, update)
let pivot;
function startup() {
pivot = findEntity('Chair').getComponent(PivotableComponent);
}
function update() {
if (pivot.isDragging) return; // never change modes mid-drag
const keys = Input.keyboard;
if (keys.isKeyReleased(Key.T)) pivot.setProps({ translate: true, rotate: false, scale: false, plane: 'XZ' });
if (keys.isKeyReleased(Key.R)) pivot.setProps({ translate: false, rotate: true, scale: false, plane: 'XZ' });
if (keys.isKeyReleased(Key.S)) pivot.setProps({ translate: false, rotate: false, scale: true, scaleAxes: { y: false } });
if (keys.isKeyReleased(Key.L)) {
pivot.setProps({ space: pivot.props.space === 'local' ? 'world' : 'local' });
}
}
  • setProps changes several props at once. T keeps only the arrows and slider on the floor plane, R keeps only the ring that turns the chair around its vertical axis, S keeps only the scale spheres, with height scaling turned off.
  • pivot.props.space reads the current prop value, so L toggles between local and world axes.
  • isDragging stops a key press from changing the gizmo while the player is using it.
SelectionReport.jx
#pragma lifecycle(startup)
const PARTS = ['Engine', 'Wheel', 'Seat', 'Handlebar'];
function startup() {
const parts = PARTS.map((name) => findEntity(name));
// Each part has a Selectable and a Signal Emitter with the signal 'parts'.
on('parts', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
const chosen = parts.find((part) => SelectableComponent.isSelected(part));
emit('ui/inspect', { name: chosen ? chosen.name : null });
});
on('ui/clear', () => {
for (const part of parts) part.getComponent(SelectableComponent).setSelected(false);
});
}
  • SelectableComponent.isSelected(entity) is a static helper: it checks any entity, without fetching its component first.
  • On every select, the script finds which part is selected and tells an inspector panel through emit.
  • The ui/clear signal deselects every part from code, for a “Clear” button.

Selectable::select is sent when the entity becomes selected. A click on an entity that is already selected changes nothing, so it sends no event. That suits a selection, where the object stays highlighted until something else is picked. For a button-like object that should react to every click, release the selection as soon as you handle it. The next click then selects it again and sends another select:

ClickCounter.jx
#pragma lifecycle(startup)
let clicks = 0;
function startup() {
// The entity's Signal Emitter puts its Selectable events on 'counter/clicked'.
on('counter/clicked', (event) => {
if (event.eventType !== EventType.Selectable.Select) return;
// Release the selection, so the next click selects it again.
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false);
clicks += 1;
log('Clicked', clicks, 'times');
});
}

setSelected(false) also sends Selectable::deselect and Selectable::change. The check on Select ignores them.

  • Expecting a second click to fire again. A click on an already selected entity sends nothing. Release the selection with setSelected(false) to react to every click (see React to every click).
  • No Signal Emitter. Without one, the component’s events go nowhere and your listener never runs. Add a Signal Emitter with a non-empty signal to the entity.
  • Expecting several selections. Selectable keeps one selection across the whole scene. For multi-select, keep your own list on Selectable::select.
  • A Pivotable that never appears. With activation: 'selection', the entity also needs a Selectable. Use activation: 'always' for a gizmo that is always shown.
  • Testing in the editor. These components run in play only. Enter play to try them.
  • Moving a grabbed body yourself. While isGrabbed is true, the Grabbable drives the body. Wait for Grabbable::end before moving it from a script.
  • Writing position on a physics body. Use setPosition, so the rigid body moves too.