Components
Updated
A component is one unit of behaviour and saved data attached to an entity. A Geometry gives an entity a shape, a Standard Material gives it a surface, a Point Light makes it shine, a Box Collider gives it a physical shape, a Selectable makes it clickable, a Behavior runs a script on it. An entity does nothing by itself: everything you see and interact with in a world comes from components. This page explains what every component has in common and how a script reads and changes components.
Before you start
Section titled “Before you start”- You know what an entity is and how to find one with
findEntity. See Working with entities. - You can write a Behavior script with a
startuphook. See Write your first script.
What every component has
Section titled “What every component has”Each component has a type string, comp-…, and a class, available to scripts as a global of the same name. The type identifies it in the editor and in event names; the class is what you pass to getComponent and what you construct with new.
| Type | Class |
|---|---|
comp- |
Point |
comp- |
Standard |
comp-outline |
Outline |
comp- |
Signal |
comp-behavior |
Behavior |
Every component, whatever its type, has these members:
| Member | What it is |
|---|---|
props |
Its current settings, as a plain object. Read it; change it only through setProps. |
set |
Change some props. Only the keys you pass change; the others keep their values. |
enabled |
Whether the component is on. |
set |
Switch the component on or off without removing it. |
type |
Its type string, such as 'comp-. |
entity |
The entity it is attached to. |
Many components add methods of their own: a rigid body has applyImpulse, a Group has recenter, an audio source can play and stop. Each component’s reference page lists them.
Props are a component’s settings: the colour of a light, the shape of a geometry, the signal of a Signal Emitter. They are what the editor shows in the properties panel and what the scene saves.
- A prop you leave out, or set to
undefined, takes its default. Each component’s reference page lists the defaults. setPropstakes a patch:light.setProps({ intensity: 2 })changes the intensity and leaves the colour alone.- Most components refuse a value outside the range their reference page lists, with an error at the moment you set it. A few clamp it instead; their pages say so.
- Changes a script makes in play are not saved: the scene starts from its saved props each time it loads.
Enabled and disabled
Section titled “Enabled and disabled”A component’s enabled flag switches it without removing it. A disabled component stays on the entity, and in the saved scene with its props, but acts as if it were not there: its effects are undone and it does nothing until it is enabled again. A disabled Outline draws no outline; a disabled Point Light gives no light; a disabled Signal Emitter emits nothing.
You can still call setProps on a disabled component: it stores the new props and applies them when it is enabled again.
Switching a component is different from switching its entity. component.setEnabled(false) turns off one feature; entity.setEnabled(false) turns off the entity, its children and every component on them.
Required, default and supported components
Section titled “Required, default and supported components”Each entity type declares three lists of components:
| List | Meaning | Example: Primitive Mesh |
|---|---|---|
| Required | Always present: they cannot be removed. Like any component they can be disabled and enabled again, except Transform, Enable, Layer and Lock, which are always enabled. | Geometry and a material, plus Enable, Layer and Lock |
| Default | What a new entity of the type starts with. You may remove them. | Shadow |
| Supported | The optional components you can add to this type. | Outline, colliders, Selectable, and others |
The entities reference lists the three for every entity type.
One per type, or several
Section titled “One per type, or several”An entity holds one component of each type. A few types allow several on one entity: the collider shapes, Signal Emitter, Enable on Signal and Terrain Texture. Use getComponents (plural) to reach all of them.
Some components also share a slot. All materials share one: an entity has one material, of whichever kind, so adding a Glass material to an entity replaces its Standard material.
Finding components from a script
Section titled “Finding components from a script”| Call | What it returns |
|---|---|
entity. |
The component of that class, or undefined. |
entity. |
The same, by type string. |
entity. |
Every component of that class, in the order added: a new array each call. For types an entity can hold several of. |
entity. |
Every component on the entity, in the order added. |
Base classes
Section titled “Base classes”Some components come in families. A base class finds a family member whatever its exact kind:
| Base class | Finds |
|---|---|
Material |
The entity’s material: Standard, Physical, Basic, Glass, Shader and the rest. |
Physics |
Every collider shape: box, sphere, capsule and the others. Use it with getComponents. |
Model |
The entity’s model loader, whatever the format: get is the model’s source. |
Dynamic |
The velocity and spring bones on an entity. Use it with getComponents. |
Other abstract families, such as cameras, have no base-class global: use the concrete class, such as PerspectiveCameraComponent.
Adding and removing components
Section titled “Adding and removing components”In the editor, select an entity and add a component from the add-component menu of the properties panel. The menu lists only the components that entity type supports, each with a one-line description. A component’s enabled toggle switches it off without removing it.
From a script, construct the component’s class with the props you want and pass it to addComponent. removeComponent takes the component itself, or a type string to remove every component of that type:
const outline = new OutlineComponent({ color: '#facc15' });crate.addComponent(outline);crate.removeComponent(outline); // this onecrate.removeComponent('comp-outline'); // or every OutlineA required component cannot be removed. Only add components the entity type supports; the add-component menu and the entities reference list them.
Reading a component reference page
Section titled “Reading a component reference page”Every component has a reference page, such as the one for Transform. They all follow the same order:
| Part | What it tells you |
|---|---|
| Description | What the component does and when to use it. |
| Props table | Each prop with its type, its default, and a description with its unit and range. A type written as a list of quoted words, such as 'enable' | 'disable' | 'toggle', accepts only those words. |
| Events | What the component sends through a Signal Emitter, as Source::action strings with their data. The same strings are EventType constants: Selectable::select is Event. Components that send nothing have no events. |
| Scripting | The component’s own methods and getters. |
Examples
Section titled “Examples”1. Recolour the object a script sits under
Section titled “1. Recolour the object a script sits under”This script changes one prop of the parent’s material, whatever kind of material it is.
#pragma lifecycle(startup)
function startup() { const target = self.parent; const material = target.getComponent(MaterialComponent); if (!material) { log(target.name, 'has no material'); return; } log('Old colour:', material.props.color); material.setProps({ color: '#f97316' });}How it works:
- The Behavior entity sits under the object, so
self.parentis the object. MaterialComponentis a base class: it finds a Standard, Physical or any other material.material.props.colorreads the current value;setPropschanges onlycolor.
2. List what an entity is made of
Section titled “2. List what an entity is made of”This script prints every component of an entity, with its type and whether it is on, and counts its colliders.
#pragma lifecycle(startup)
function startup() { const crate = findEntity('Crate'); if (!crate) return;
crate.getAllComponents().forEach((component) => { log(component.type, component.enabled ? 'on' : 'off'); });
const colliders = crate.getComponents(PhysicsColliderComponent); log(crate.name, 'has', colliders.length, 'collider shape(s)');}How it works:
getAllComponents()returns every component in the order it was added, required ones included.component.typeis thecomp-…string;component.enabledits on/off state.getComponents(PhysicsColliderComponent)gathers every collider shape, whatever its kind.
3. Show an outline while the pointer is over an object
Section titled “3. Show an outline while the pointer is over an object”The object has a Selectable and a Signal Emitter on the signal crate/pointer. The script adds a disabled Outline once, then switches it on and off as the pointer comes and goes.
#pragma lifecycle(startup)
function startup() { const crate = findEntity('Crate'); if (!crate) return;
const outline = new OutlineComponent({ color: '#38bdf8', thickness: 6 }); crate.addComponent(outline); outline.setEnabled(false);
on('crate/pointer', (event) => { if (event.eventType === EventType.Selectable.Hover) outline.setEnabled(true); if (event.eventType === EventType.Selectable.Leave) outline.setEnabled(false); });}A script adds a disabled Outline to a crate once, then switches it on and off from the Selectable hover and leave events.
Move the pointer over the crate. Drag to orbit the camera, scroll to zoom.
How it works:
- The Outline is created once, so hovering only switches it, which is cheap.
- The Selectable sends
Selectable::hoverandSelectable::leave; the Signal Emitter puts them oncrate/pointer. setEnabledturns the outline on and off without removing it, so its props are kept.
4. Flash a light when a button is pressed, then restore it
Section titled “4. Flash a light when a button is pressed, then restore it”A realistic pattern: remember a component’s props, change them for a moment, and put them back.
#pragma lifecycle(startup)
let busy = false;
function flash(light) { if (busy) return; busy = true; const saved = { color: light.props.color, intensity: light.props.intensity }; light.setProps({ color: '#ef4444', intensity: saved.intensity * 4 }); delay(() => { light.setProps(saved); busy = false; }, 300);}
function startup() { const lamp = findEntity('Alarm Lamp'); const light = lamp && lamp.getComponent(PointLightComponent); if (!light) { log('No point light named Alarm Lamp'); return; }
on('alarm/button', (event) => { if (event.eventType === EventType.UIButton.Press) flash(light); });}How it works:
- A UI button’s Signal Emitter puts its
UIButton::pressevents onalarm/button. savedcopies the two props that change, sosetProps(saved)restores exactly those.delayrestores the light 300 ms later; thebusyflag ignores presses during a flash.flashis declared abovestartup, which calls it.
Common mistakes
Section titled “Common mistakes”Writing to props directly
Section titled “Writing to props directly”props is for reading. Assigning to it does not tell the component, so nothing updates.
light.props.intensity = 3;light.setProps({ intensity: 3 });Assuming a component exists
Section titled “Assuming a component exists”getComponent returns undefined when the entity has no such component, and calling a method on undefined stops the script.
findEntity('Lamp').getComponent(PointLightComponent).setProps({ intensity: 2 });const light = findEntity('Lamp')?.getComponent(PointLightComponent);if (light) light.setProps({ intensity: 2 });Removing a component to turn it off
Section titled “Removing a component to turn it off”Removing a component loses its props, and required components cannot be removed at all. To pause a feature, disable it.
crate.removeComponent('comp-outline');// later: the outline's colour and thickness are gonecrate.getComponent(OutlineComponent).setEnabled(false);// latercrate.getComponent(OutlineComponent).setEnabled(true);Expecting two materials
Section titled “Expecting two materials”Materials share one slot. Adding a second material replaces the first, it does not layer on top of it. To change the look, call setProps on the existing material, or add the new kind deliberately to replace it.