Skip to content

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.

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-light PointLightComponent
comp-standard-material StandardMaterialComponent
comp-outline OutlineComponent
comp-signal-emitter SignalEmitterComponent
comp-behavior BehaviorComponent

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.
setProps(patch) Change some props. Only the keys you pass change; the others keep their values.
enabled Whether the component is on.
setEnabled(on) Switch the component on or off without removing it.
type Its type string, such as 'comp-point-light'.
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.
  • setProps takes 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.

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.

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.

Call What it returns
entity.getComponent(Class) The component of that class, or undefined.
entity.getComponent('comp-…') The same, by type string.
entity.getComponents(Class) Every component of that class, in the order added: a new array each call. For types an entity can hold several of.
entity.getAllComponents() Every component on the entity, in the order added.

Some components come in families. A base class finds a family member whatever its exact kind:

Base class Finds
MaterialComponent The entity’s material: Standard, Physical, Basic, Glass, Shader and the rest.
PhysicsColliderComponent Every collider shape: box, sphere, capsule and the others. Use it with getComponents.
ModelLoaderComponent The entity’s model loader, whatever the format: getComponent(ModelLoaderComponent).props.src is the model’s source.
DynamicBoneComponent 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.

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 one
crate.removeComponent('comp-outline'); // or every Outline

A required component cannot be removed. Only add components the entity type supports; the add-component menu and the entities reference list them.

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 EventType.Selectable.Select. Components that send nothing have no events.
Scripting The component’s own methods and getters.

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.

Tint.jx
#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.parent is the object.
  • MaterialComponent is a base class: it finds a Standard, Physical or any other material.
  • material.props.color reads the current value; setProps changes only color.

This script prints every component of an entity, with its type and whether it is on, and counts its colliders.

Inspect.jx
#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.type is the comp-… string; component.enabled its 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.

HoverOutline.jx
#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);
});
}

How it works:

  • The Outline is created once, so hovering only switches it, which is cheap.
  • The Selectable sends Selectable::hover and Selectable::leave; the Signal Emitter puts them on crate/pointer.
  • setEnabled turns 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.

Flash.jx
#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::press events on alarm/button.
  • saved copies the two props that change, so setProps(saved) restores exactly those.
  • delay restores the light 300 ms later; the busy flag ignores presses during a flash.
  • flash is declared above startup, which calls it.

props is for reading. Assigning to it does not tell the component, so nothing updates.

Wrong
light.props.intensity = 3;
Right
light.setProps({ intensity: 3 });

getComponent returns undefined when the entity has no such component, and calling a method on undefined stops the script.

Wrong
findEntity('Lamp').getComponent(PointLightComponent).setProps({ intensity: 2 });
Right
const light = findEntity('Lamp')?.getComponent(PointLightComponent);
if (light) light.setProps({ intensity: 2 });

Removing a component loses its props, and required components cannot be removed at all. To pause a feature, disable it.

Wrong
crate.removeComponent('comp-outline');
// later: the outline's colour and thickness are gone
Right
crate.getComponent(OutlineComponent).setEnabled(false);
// later
crate.getComponent(OutlineComponent).setEnabled(true);

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.