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.
Before you start
Section titled “Before you start”- You know which layers exist and what their settings do. See Layers and Layer settings.
- You can find entities from a script. See Working with entities.
| Call | What it does |
|---|---|
entity. |
Put the entity, and what it builds or loads, on user layer n, 0 to 20. Returns the entity. |
entity. |
The entity’s current layer. |
entity. |
The same as setLayer, through the component. |
Events
Section titled “Events”When an entity’s layer changes, its Layer component sends an event through the entity’s Signal Emitters:
| Event | EventType |
Data |
|---|---|---|
Layer::change |
Event |
{ layer }: the new layer, 0 to 20. |
Examples
Section titled “Examples”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.
#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');}The H key shows or hides every decoration: the script finds the entities whose Layer component is 6 and switches them with setEnabled.
Click the scene, then press H to hide and show the flowers. Drag to orbit the camera, scroll to zoom.
How it works:
collectwalks the scene once, instartup, and keeps the entities on layer 6.isKeyReleased(Key.H)is true for one frame per press, so each press toggles once.setEnabledswitches each decoration and everything under it, and pauses their scripts.- Entity references stay valid across frames, so
updateonly loops over a short list.
Snippets
Section titled “Snippets”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.
#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’slayerprop.LayerComponenthas alayergetter that returns the current value.- Whatever layer 3 is set to (locked, raycastable…) now applies to the crate.
2. Report layer changes
Section titled “2. Report layer changes”This script gives an entity a Signal Emitter for its layer events, then reports each change, whoever makes it.
#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 toeventFilter. 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.
Common mistakes
Section titled “Common mistakes”Using a reserved layer
Section titled “Using a reserved layer”Layers 21 to 31 belong to the runtime and are rejected with an error.
beam.setLayer(30);beam.setLayer(15); // a user layer the project defines as not raycastable