Skip to content

Layers from a script

Updated

A script changes which layer an entity is on. Layer definitions themselves (labels, locked, enabled, raycastable) are project settings, made while authoring.

Call What it does
entity.setLayer(n) Put the entity, and what it builds or loads, on user layer n, 0 to 20. Returns the entity.
entity.getComponent(LayerComponent).layer The entity’s current layer.
entity.getComponent(LayerComponent).setProps({ layer: n }) The same as setLayer, through the component.

When an entity’s layer changes, its Layer component sends an event through the entity’s Signal Emitters:

Event EventType Data
Layer::change EventType.Layer.Change { layer }: the new layer, 0 to 20.

1. Switch a whole layer of objects on and off

Section titled “1. Switch a whole layer of objects on and off”

A realistic pattern: the H key shows or hides every decoration in the scene. The decorations are all on layer 6, and the script finds them by their layer.

ToggleDecor.jx
#pragma lifecycle(startup, update)
const DECOR_LAYER = 6;
let decor = [];
let shown = true;
function collect() {
decor = [];
scene.entities.forEach((entity) => {
const layer = entity.getComponent(LayerComponent);
if (layer && layer.layer === DECOR_LAYER) decor.push(entity);
});
}
function startup() {
collect();
log('Decorations found:', decor.length);
}
function update() {
if (!Input.keyboard.isKeyReleased(Key.H)) return;
shown = !shown;
decor.forEach((entity) => entity.setEnabled(shown));
log('Decorations', shown ? 'on' : 'off');
}

How it works:

  • collect walks the scene once, in startup, and keeps the entities on layer 6.
  • isKeyReleased(Key.H) is true for one frame per press, so each press toggles once.
  • setEnabled switches each decoration and everything under it, and pauses their scripts.
  • Entity references stay valid across frames, so update only loops over a short list.

1. Move an entity to a layer and read it back

Section titled “1. Move an entity to a layer and read it back”

The simplest use: put an entity on a layer and confirm it.

SetLayer.jx
#pragma lifecycle(startup)
function startup() {
const crate = findEntity('Crate');
if (!crate) return;
crate.setLayer(3);
log(crate.name, 'is on layer', crate.getComponent(LayerComponent).layer);
}

How it works:

  • setLayer(3) is a shortcut for the Layer component’s layer prop.
  • LayerComponent has a layer getter that returns the current value.
  • Whatever layer 3 is set to (locked, raycastable…) now applies to the crate.

This script gives an entity a Signal Emitter for its layer events, then reports each change, whoever makes it.

LayerWatch.jx
#pragma lifecycle(startup)
function startup() {
const crate = findEntity('Crate');
if (!crate) return;
crate.addComponent(new SignalEmitterComponent({ signal: 'crate/layer', eventFilter: ['Layer::change'] }));
on('crate/layer', (event) => {
if (event.eventType === EventType.Layer.Change) log('Crate moved to layer', event.data.layer);
});
crate.setLayer(4);
}

How it works:

  • The Signal Emitter forwards only Layer::change, thanks to eventFilter.
  • crate.setLayer(4) triggers the event, so the script logs it right away.
  • Any other script that moves the crate to another layer is reported too.

Layers 21 to 31 belong to the runtime and are rejected with an error.

Wrong
beam.setLayer(30);
Right
beam.setLayer(15); // a user layer the project defines as not raycastable