Controls
Updated
Controls are the UI elements a player presses, drags or types into: buttons, checkboxes, switches, toggle buttons, sliders, progress bars, text inputs and the in-scene keyboard, lists, menus and tooltips. Each control reports what happened as an event through a Signal Emitter on its entity, and each has methods a script calls to change it. This page covers how to wire a control to a script, then every control with its props, methods and events.
Before you start
Section titled “Before you start”- Read the UI overview for UI entities, roots and the theme.
- Controls usually sit in a panel. See Panels and layout.
- Events travel on the event bus. See Scripting for
on,emitand the lifecycle hooks, and Events and signals for Signal Emitters.
Wiring events
Section titled “Wiring events”A control sends its events only while its entity has a Signal Emitter (comp-signal-emitter). The emitter puts each event on the event bus under its signal, and a Behavior hears it with on(signal, callback).
- Add a Signal Emitter to the control’s entity and set its
signal, for examplesettings:volume. A prefix such assettings:keeps your signals apart. - In a Behavior, register a listener in
startup:on('settings:volume', (event) => { … }). - In the callback, compare
event.eventTypewith anEventTypeconstant and readevent.data.
Every event has the shape { entityId, eventType, data }:
entityIdis the id of the entity that sent it;findEntity(event.entityId)returns the entity.eventTypeis'<Component>::<action>'. TheEventTypeglobal names each one:EventType.UISlider.Changeis'UISlider::change'.dataholds the details, such as{ value }or{ checked }.
Several controls can share one signal; check eventType or entityId to tell them apart. An emitter’s eventFilter lets only the listed event types through. You can also add the emitter from a script: entity.addComponent(new SignalEmitterComponent({ signal: 'menu/play' })).
| Event | EventType |
Data | Sent when |
|---|---|---|---|
UIButton::press |
Event |
{} |
A completed press, when the button has no Link. |
UICheckbox::change |
Event |
{ checked } |
The state flips, by a press or setChecked. |
UISwitch::change |
Event |
{ checked } |
The state flips. |
UIToggle |
Event |
{ checked } |
The state flips. |
UISlider::change |
Event |
{ value } |
The value changes: each step of a drag, or setValue. |
UISlider::change- |
Event |
{ value } |
A drag or track press is released with a different value. |
UIProgress::change |
Event |
{ value } |
The shown value changes. Nothing while indeterminate. |
UIInput::change |
Event |
{ text } |
The text changes. |
UIInput::submit |
Event |
{ text } |
Enter, or the keyboard’s enter key. |
UIInput::focus, UIInput::blur |
Event, Event |
{ text } |
The field gains or loses focus. |
UIKeyboard::press |
Event |
{ key } |
A key of the in-scene keyboard: the character, or backspace, space or enter. |
UIKeyboard::show, UIKeyboard::hide |
Event, Event |
{} |
The keyboard appears or goes. |
UIList::select, UIList::deselect |
Event, Event |
{ value } |
A row of the list is selected or deselected. Sent by the list entity. |
UIList |
Event |
{ value } |
A row is pressed. Sent by the row entity. |
UIMenu::open, UIMenu::close |
Event, Event |
{} |
The menu popup opens or closes. |
UIMenu |
Event |
{ value } |
A menu entry is chosen. |
UITooltip::show, UITooltip::hide |
Event, Event |
{} |
The tooltip bubble appears or goes. |
Common to all controls:
- Editor and play. Controls are inert while you edit and react in play.
- Disabled. A control with
disabled: trueis dimmed and ignores input. Disabling the component itself instead takes the control out of the layout. - Labels. An explicitly empty
texton a UI Input, Switch, Slider, Progress or Checkbox draws no label at all. - Multiplayer. The state of a UI Checkbox, Switch, Toggle Button, Slider and Progress (and a UI Label) can be shared with everyone in a room as a network trait, so every player sees the same value. See Multiplayer.
UI Button
Section titled “UI Button”The UI Button component (comp-ui-button) is a button with a label, an icon, or both. Entity: UI Button (ent-ui-button), created with the text Button.
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | none | Button text. Empty: no caption. |
icon |
string | none | An icon name, such as 'Trash2' or 'Chat', shown with the label or alone. |
iconPosition |
'left', 'right', 'top', 'bottom' |
'left' |
Where the icon sits relative to the label. |
variant |
'solid', 'subtle', 'bare' |
'solid' |
solid is filled; subtle shows a fill on hover only; bare is the content alone. |
align |
'left', 'center', 'right' |
'center' |
Where icon and label sit in a button wider than its content. |
fontSize |
number | 16 from the theme |
Label size in px. |
font |
string | the theme’s | Font family, by name. See Placement and fonts. |
size |
number | fontSize |
Icon size in px. |
sizeScale |
number | 1 |
Multiplies the theme’s font size and padding. Explicit fontSize and padding win. |
single |
boolean | false |
Keep the whole label on one line. |
color |
string | the theme’s foreground | Label and icon colour. |
background |
string | the theme’s primary colour | Fill, for solid. |
hoverColor, pressedColor |
string | from the theme | Fill on hover and while held. |
padding, margin |
number or number[] | 12, none |
In px. See Spacing. |
borderRadius |
number or number[] | 10 from the theme |
Corner radius in px. |
borderColor, borderWidth |
string, number or number[] | none, 2 |
Outline, drawn only when borderColor is set. |
badge, badgeColor |
number, string | none | A count in a badge at the top right. 0 or less: no badge. |
indicator |
number | none | A count near the lower-right edge, shown as 99+ above 99. |
unread |
boolean | false |
An unread dot near the upper-right edge. |
hover |
boolean | false |
A disabled button still shows its tooltip. |
disabled |
boolean | false |
Dim the button and ignore presses. |
| Method | What it does |
|---|---|
press() |
Act as a pointer press would; nothing while disabled. |
setText(text) |
Change the label. |
set |
Change the badge count. |
set |
Disable or enable the button. |
- Press and release. A press counts when released over the same button. Releasing outside cancels it; you can move back over the button before releasing.
- Link. With a Link component on the entity, a press opens the link instead of sending
UIButton::press. - Decorative buttons. A
subtleorbarebutton with no Signal Emitter, Link or menu takes no pointer at all, so decorative icons never block what is behind them.
UI Checkbox
Section titled “UI Checkbox”The UI Checkbox component (comp-ui-checkbox) is a box with a label that turns on and off. With radio: true it draws a ring and dot, for radio buttons in a UI Selection Group. Entity: UI Checkbox (ent-ui-checkbox).
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | 'Checkbox' |
Label beside the box. |
checked |
boolean | false |
Whether it is on. |
color |
string | from the theme | One colour for the border, label and mark. |
size |
number | 24 |
Width and height of the box in px. |
radio |
boolean | false |
Round ring and dot instead of a square box. |
padding, margin |
number or number[] | none | In px. |
disabled |
boolean | false |
Dim the control and ignore presses. |
Methods: toggle(), setChecked(checked), isChecked(). Event: UICheckbox::change { checked }.
UI Switch
Section titled “UI Switch”The UI Switch component (comp-ui-switch) is an on/off switch: a pill track with a sliding handle and an optional label. Entity: UI Switch (ent-ui-switch), created with the text Switch.
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | none | Label beside the track. |
checked |
boolean | false |
Whether it is on. |
color |
string | the theme’s primary colour | Track colour while on. |
padding, margin |
number or number[] | none | In px. |
disabled |
boolean | false |
Dim the control and ignore presses. |
Methods: toggle(), setChecked(checked), isChecked(). Event: UISwitch::change { checked }.
UI Toggle Button
Section titled “UI Toggle Button”The UI Toggle Button component (comp-ui-toggle-button) is a button that latches: a press flips checked, and the fill stays lit until the next press. Use it for state, such as a microphone on/off or the tool picked in a toolbar; group several with a UI Selection Group. Entity: UI Toggle Button (ent-ui-toggle-button), created with the text Toggle.
It takes the UI Button’s text, icon, iconPosition, align, size, fontSize, color, sizeScale, padding, margin, borderRadius, borderColor, borderWidth, badge and badgeColor, plus:
| Prop | Type | Default | Description |
|---|---|---|---|
checked |
boolean | false |
Whether it is on. |
background |
string | the theme’s muted colour | Fill while off. |
checkedColor |
string | the theme’s primary colour | Fill while on. |
hoverColor, pressedColor |
string | from the theme | Fill on hover and while held. |
disabled |
boolean | false |
Dim the button and ignore presses. |
Methods: toggle(), setChecked(checked), isChecked(), setBadge(count). Event: UIToggleButton::change { checked }.
UI Slider
Section titled “UI Slider”The UI Slider component (comp-ui-slider) picks a number by dragging: a header with the label and value, over a track with a fill and a thumb. Entity: UI Slider (ent-ui-slider).
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | none | Header label, left of the value. |
value |
number | min |
Current value, stepped and kept within min to max. |
min, max |
number | 0, 100 |
The range. |
step |
number | 1 |
Step the value snaps to. 0 for a continuous value. |
showValue |
boolean | true |
Show the value in the header. |
unit |
string | none | Unit after the shown value, such as '%'. |
color |
string | the theme’s primary colour | Fill and thumb colour. |
padding, margin |
number or number[] | none | In px. |
disabled |
boolean | false |
Dim the slider and ignore input. |
Methods: getValue(), setValue(value). Events: UISlider::change { value } on every stepped change, and UISlider::change-end { value } once a drag is released with a different value. Pressing the track jumps to that value; pressing the thumb keeps its value until the drag moves it. A drag can continue outside the track.
Use change for live feedback (a volume that follows the thumb) and change-end for work that should run once (saving a setting, sending it over the network).
UI Progress
Section titled “UI Progress”The UI Progress component (comp-ui-progress) shows progress as a bar or a ring, with a known value or an indeterminate animation. It takes no pointer. Entity: UI Progress (ent-ui-progress).
| Prop | Type | Default | Description |
|---|---|---|---|
type |
'linear' or 'circular' |
'linear' |
A bar or a ring. |
text |
string | none | Header label. Linear only. |
value |
number | min |
Current progress, kept within min to max. |
min, max |
number | 0, 100 |
The range. |
showValue |
boolean | true |
Show the value beside the label or in the ring. |
unit |
string | none | Unit after the shown value. |
color |
string | the theme’s primary colour | Fill or arc colour. |
size |
number | 64 |
Ring diameter in px. Circular only. |
indeterminate |
boolean | false |
Unknown duration: a segment sweeps along the bar, or the arc spins. value is ignored. |
direction |
'forward' or 'reverse' |
'forward' |
Forward fills left to right, or clockwise from 12 o’clock. |
padding, margin |
number or number[] | none | In px. |
Methods: getValue(), setValue(value) (no effect while indeterminate), setIndeterminate(on), isIndeterminate(). Event: UIProgress::change { value }. The indeterminate motion animates in play only.
UI Input
Section titled “UI Input”The UI Input component (comp-ui-input) is a text field with an optional header label. It keeps its own text: read it with getValue() or from its events. Entity: UI Input (ent-ui-input).
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | none | Header label above the field. |
value |
string | '' |
Current text. |
placeholder |
string | none | Grey text shown while empty. |
icon |
string | none | A decorative icon name inside the field. |
type |
'text', 'password', 'number' |
'text' |
Plain, masked, or numeric. |
color |
string | the theme’s primary colour | Focus ring colour. |
maxLength |
number | 50 |
Most characters accepted. |
maxRows |
number | 1 |
Lines visible before the text scrolls. Above 1 the field is multiline. |
clearable |
boolean | false |
Show a clear button while the field has text. |
virtual |
boolean | false |
Show the in-scene keyboard on desktop and mobile too. Ignored in VR. |
embedded |
boolean | false |
Use the surrounding UI for the field’s border and background. |
focus |
boolean | false |
A press on the outer surface or padding focuses the field. |
flexGrow |
number | none | Share of the free space in a row. |
padding, margin |
number or number[] | [8, 12], none |
In px. |
disabled |
boolean | false |
Dim the field and ignore input. |
Methods: getValue(), setValue(text) (cut to maxLength, sends change), and the isFocused getter. Events: UIInput::change, UIInput::submit, UIInput::focus and UIInput::blur, each { text }.
- Enter sends
submitand closes the keyboard. In a multiline field, Shift+Enter adds a line instead. - Numbers. A
numberfield keeps its value as text and asks for the numeric keyboard. The type is a keyboard hint, not validation: check the text yourself. Up and Down add or subtract 1. - Focus. A press outside the field and its keyboard drops focus, in XR too.
- Password. A
passwordfield masks what is typed;getValue()still returns the real text in play.
Which keyboard opens
Section titled “Which keyboard opens”- In XR, a focused field uses the headset’s system keyboard when there is one. Otherwise the in-scene keyboard appears just below the field’s panel, tilted like a desk keyboard.
- On desktop and mobile, the physical or on-screen keyboard of the device types. With
virtualKeyboard: truethe in-scene keyboard appears inside the view instead, for example on a touch kiosk.
The in-scene keyboard the runtime opens is one shared UI Keyboard, reused for every field. It shows the numeric layout for a number field and QWERTY for the rest.
UI Keyboard
Section titled “UI Keyboard”The UI Keyboard entity (ent-ui-keyboard) is the in-scene keyboard. Its keys type into whichever UI Input has focus, so one keyboard serves every field. Place one in a scene for a fixed keyboard, such as on a kiosk, or let the runtime open its own. It carries one of two layouts:
A numeric UI Keyboard placed in the scene types into a number field; a Behavior script checks the code on submit and swings the vault door open with a tween.
Click the code field, then press 2, 4, 6, 8 and the enter key on the in-scene keypad. A wrong code clears the field. Drag outside the panel to orbit the camera, scroll to zoom.
- UI Keyboard QWERTY (
comp-ui-keyboard-qwerty, the default): letters, digits, backspace, space and enter. Shift capitalizes the next letter; ?123 opens a page of symbols and ABC returns to letters. - UI Keyboard Numeric (
comp-ui-keyboard-numeric):0to9,.,-, backspace and enter.
Scripts reach either layout as UIKeyboardComponent.
| Prop | Type | Default | Description |
|---|---|---|---|
color |
string | from the theme | Key hover colour. |
opacity |
number | 0.98 |
Alpha of the keyboard’s fill. |
padding, margin |
number or number[] | 10, none |
In px. |
Methods: show(), hide(), isShown(). Events: UIKeyboard::press { key }, UIKeyboard::show and UIKeyboard::hide. Shift and page switches do not send press. Every keyboard entity, including the one the runtime opens, comes with a Signal Emitter on the signal ui:keyboard, so you can hear key presses without any setup.
UI List
Section titled “UI List”The UI List component (comp-ui-list) turns a layout into a scrolling list of selectable rows. The rows are UI List Item entities below it. Entity: UI List (ent-ui-list), a layout with no gap or padding so the rows read as one list. Add a UI Panel component to it for a fill.
| Prop | Type | Default | Description |
|---|---|---|---|
selection |
'none', 'single', 'multiple' |
'single' |
How many rows can be selected. none leaves rows as plain actions. |
scrollbar |
boolean | true |
Draw a scrollbar while the content overflows. |
scrollbar |
number | 6 |
Scrollbar thickness in px. |
scrollbar |
string | from the theme | Scrollbar colour. |
- Scrolling. Set the list’s UI Layout
heightormaxHeightto make overflowing rows scroll. Touch and controller drags can start on a row; a tap still presses it. - Selection. In
single, pressing a row selects it and deselects the rest. Inmultiple, a press flips the row. - Methods. On the component:
selectedValues()returns the selected rows’ values,getItems()returns the rows. On the UI List entity:selectedValues(),scrollTo(x, y)to scroll to an offset in layout px, and thescrollPositiongetter. - Events.
UIList::selectandUIList::deselect{ value }, sent from the list entity, so one Signal Emitter on the list covers every row.
UI List Item
Section titled “UI List Item”The UI List Item component (comp-ui-list-item) is one row of the nearest UI List above it: a left-aligned, subtle button that also shows a selected state. Entity: UI List Item (ent-ui-list-item), created with the text Item.
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | none | Row text. |
value |
string | the entity’s name | Identifier reported when the row is picked. |
selected |
boolean | false |
Whether the row starts selected. |
icon, iconPosition, size |
as UI Button | Icon, its side and size. | |
align |
'left', 'center', 'right' |
'left' |
Where icon and label sit. |
fontSize, color, padding, margin, borderRadius |
as UI Button | Label and box. | |
background |
string | none | Resting fill; absent, the row is transparent. |
selectedColor |
string | the theme’s primary colour | Fill while selected. |
hoverColor, pressedColor |
string | from the theme | Fill on hover and while held. |
badge, badgeColor |
as UI Button | Badge count and fill. | |
disabled |
boolean | false |
Dim the row and ignore presses. |
Methods: isChecked(), setChecked(selected), and the value getter. Event: UIListItem::select { value } on every press of the row, whatever the list’s mode.
UI Menu
Section titled “UI Menu”The UI Menu component (comp-ui-menu) makes its entity a menu trigger: pressing it opens a popup, and every UI element below the entity goes into the popup. The UI Menu entity (ent-ui-menu) is a UI Button with a UI Menu; the component also works on a UI Label or UI Avatar. Its entries are UI Menu Item entities.
| Prop | Type | Default | Description |
|---|---|---|---|
trigger |
'select' or 'hover' |
'select' |
What opens the menu: a press, or hovering. |
placement |
'top', 'bottom', 'left', 'right' |
'bottom' |
Side of the trigger the popup opens on. |
alignment |
'start', 'center', 'end' |
'center' |
Alignment along that side. |
layout |
'vertical' or 'horizontal' |
'vertical' |
Direction the entries flow. |
gap |
number | 4 |
Space between entries, in px. |
offset |
number | 4 |
Space between the trigger and the popup, in px. |
padding |
number or number[] | 6 |
Popup padding in px. |
minWidth |
number | 160 |
Minimum popup width in px. |
background, opacity |
string, number | the theme’s surface, 0.95 |
Popup fill and its alpha. |
borderRadius, borderColor, borderWidth |
10, none, 2 |
Popup corners and outline. | |
open |
boolean | false |
Whether the menu starts open. |
- Methods. On the component:
open(),close(),toggle(),setOpen(open),isOpen(),getItems(), andsetPlacement(side)to move the popup to another side for now (undefinedgoes back to the prop). On the UI Menu entity:open(),close(),toggle(),isOpen()anditems(). - Closing. The menu closes after an entry is chosen, on a press anywhere outside it, and when its scene leaves. A
hovermenu closes a quarter second after the pointer leaves both trigger and popup. - Submenus. A UI Menu on a menu item makes that entry open a submenu.
- Events.
UIMenu::openandUIMenu::close{}. A menu authoredopensends nothing on load.
UI Menu Item
Section titled “UI Menu Item”The UI Menu Item component (comp-ui-menu-item) marks its entity as an entry of the nearest UI Menu above it. It draws nothing itself: the look comes from the entity’s button. The UI Menu Item entity (ent-ui-menu-item) is a UI Button with a UI Menu Item.
| Prop | Type | Default | Description |
|---|---|---|---|
value |
string | the entity’s name | Identifier reported when the entry is chosen. |
Event: UIMenuItem::select { value }. The button also sends its own UIButton::press, so listen for one or the other. An entry that opens a submenu sends nothing.
UI Tooltip
Section titled “UI Tooltip”The UI Tooltip component (comp-ui-tooltip) shows a text bubble beside a UI element while a pointer rests on it. Add it to any control or panel; the element itself is the trigger.
| Prop | Type | Default | Description |
|---|---|---|---|
text |
string | '' |
Bubble text. Empty: no tooltip. |
placement |
'top', 'bottom', 'left', 'right' |
'top' |
Side of the element the bubble sits on. |
offset |
number | 8 |
Space between the element and the bubble, in px. |
delay |
number | 1000 |
Hover time before the bubble shows, in milliseconds. |
color, background |
string | from the theme | Text and bubble colours. |
padding |
number or number[] | [6, 12] |
Bubble padding in px. |
Methods: show() shows the bubble at once, skipping the delay; hide(). Events: UITooltip::show and UITooltip::hide {}. Tooltips need hover, which mouse and XR rays have and touch screens do not: on mobile, call show() from a script when the hint matters.
Examples
Section titled “Examples”1. A settings panel
Section titled “1. A settings panel”#pragma lifecycle(startup)
function startup() { const music = findEntity('Music').getComponent(AudioComponent); const slider = findEntity('Volume').getComponent(UISliderComponent); const mute = findEntity('Mute').getComponent(UISwitchComponent);
slider.setProps({ text: 'Volume', min: 0, max: 100, step: 5, unit: '%' }); slider.setValue(80);
function apply() { music.setVolume(mute.isChecked() ? 0 : slider.getValue() / 100); }
on('settings:volume', (event) => { if (event.eventType === EventType.UISlider.Change) apply(); }); on('settings:mute', (event) => { if (event.eventType === EventType.UISwitch.Change) apply(); }); on('settings:reset', (event) => { if (event.eventType !== EventType.UIButton.Press) return; mute.setChecked(false); slider.setValue(80); });}A settings panel whose volume slider, mute switch and reset button drive the music: UI Slider, UI Switch and UI Button events handled by one Behavior script that sets an Audio volume.
With your sound on, move the slider, flip Mute and press Reset. Drag outside the panel to orbit the camera, scroll to zoom. The music is a short loop generated for this example.
How it works:
- Each control has its own Signal Emitter and signal, so each listener only handles its control.
- One
applyfunction computes the volume from the current state of both controls, so the slider and the switch can never disagree. - The Reset button calls
setCheckedandsetValue. Those send their ownchangeevents, which run the other two listeners and callapplyagain: the music follows without extra code.
2. A shop button with a badge
Section titled “2. A shop button with a badge”#pragma lifecycle(startup)
// Scene: a UI Button named 'Buy' (Signal Emitter: 'shop/buy')// and a UI Label named 'Coins'. Other scripts emit 'coin' when the player picks one up.const PRICE = 5;let coins = 0;let owned = 0;
function startup() { const buy = findEntity('Buy').getComponent(UIButtonComponent); const label = findEntity('Coins').getComponent(UILabelComponent);
function refresh() { label.setProps({ text: `Coins: ${coins}` }); buy.setText(`Buy torch (${PRICE})`); buy.setBadge(owned); buy.setDisabled(coins < PRICE); }
on('coin', () => { coins += 1; refresh(); });
on('shop/buy', (event) => { if (event.eventType !== EventType.UIButton.Press) return; coins -= PRICE; owned += 1; refresh(); });
refresh();}A Buy button shows its price in its text and how many torches you own in its badge, and is disabled until you have enough coins.
Press Pick up a coin five times to afford a torch, then press Buy. Drag outside the panel to orbit the camera, scroll to zoom.
How it works:
setDisabled(true)dims the button and ignores presses, so the player cannot buy without enough coins; the press listener never runs then.setBadge(owned)shows how many torches the player owns in the badge; a count of 0 hides it.- All state lives in plain variables, and
refreshredraws every element from them after each change.
3. A name field
Section titled “3. A name field”#pragma lifecycle(startup)
function startup() { const input = findEntity('Name').getComponent(UIInputComponent); const greeting = findEntity('Greeting').getComponent(UILabelComponent);
input.setProps({ text: 'Your name', placeholder: 'Type and press Enter', maxLength: 20, clearable: true });
on('profile:name', (event) => { if (event.eventType === EventType.UIInput.Change) { greeting.setProps({ text: event.data.text ? `Hello, ${event.data.text}…` : '' }); } if (event.eventType === EventType.UIInput.Submit) { const name = event.data.text.trim(); if (!name) return; greeting.setProps({ text: `Welcome, ${name}!`, color: '#4ade80' }); emit('profile/named', { name }); } });}A UI Input that greets the player as they type and confirms the name on Enter: a Behavior script listens to the field's change and submit events.
Click the field and type a name: the greeting follows each key. Press Enter to confirm it in green. Drag outside the panel to orbit the camera, scroll to zoom.
How it works:
UIInput::changearrives on every keystroke with the whole text, so the greeting follows as the player types.UIInput::submitarrives when the player presses Enter (or the in-scene keyboard’s enter key). The script trims the text, ignores an empty name, and announces it to other scripts withemit.- The same code works in a headset: focusing the field opens the system keyboard or the in-scene keyboard automatically.
4. A loading bar
Section titled “4. A loading bar”#pragma lifecycle(startup)
async function startup() { const bar = findEntity('Loading').getComponent(UIProgressComponent); const status = findEntity('Status').getComponent(UILabelComponent);
// Unknown duration first: a sweeping segment. bar.setProps({ text: 'Preparing', unit: '%' }); bar.setIndeterminate(true); status.setProps({ text: 'Connecting…' }); await new Promise((done) => delay(done, 1500));
// Then a known value, from 0 to 100 over three seconds. bar.setIndeterminate(false); bar.setProps({ text: 'Loading' }); await Tween.value(0, 100, (value) => bar.setValue(Math.round(value)), { duration: 3 });
status.setProps({ text: 'Ready' });}A UI Progress bar that first sweeps while the wait is unknown, then fills from 0 to 100% with a tween, with a status label beside it.
Watch the bar sweep while it connects, then fill. Run the example again to replay it. Drag to orbit the camera, scroll to zoom.
How it works:
startupisasync, so it canawait.delay(done, 1500)resolves the promise after 1.5 seconds of game time.setIndeterminate(true)shows an animation for a wait of unknown length;setValuehas no effect until it is turned off.Tween.valueruns the callback with values from 0 to 100 over three seconds, andsetValuemoves the bar. Only the fill is redrawn on each change.
5. Choosing a level from a list
Section titled “5. Choosing a level from a list”#pragma lifecycle(startup)
function startup() { const list = findEntity('Levels').getComponent(UIListComponent); const play = findEntity('Play').getComponent(UIButtonComponent); play.setDisabled(true);
on('levels', (event) => { if (event.eventType === EventType.UIList.Select) { play.setText(`Play ${event.data.value}`); play.setDisabled(false); } });
on('levels/play', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; const [level] = list.selectedValues(); if (level) await Scenes.load(level); });}A UI List of levels with icons and a locked row: selecting a row enables the Play button, and Play loads the scene whose id is the row's value.
Pick a level in the list, then press Play to load its scene. Frozen Peak is disabled. Run the example again to choose another level. Drag outside the panel to orbit the camera, scroll to zoom.
How it works:
- One Signal Emitter on the list entity reports every row, because
UIList::selectcomes from the list, not from the row. - The Play button stays disabled until something is selected.
- When Play is pressed,
selectedValues()returns the values of the selected rows; insinglemode there is at most one.Scenes.loadswitches to the scene with that id.
6. A menu with a tooltip and key clicks
Section titled “6. A menu with a tooltip and key clicks”#pragma lifecycle(startup)
function startup() { const fileEntity = findEntity('File'); const menu = fileEntity.getComponent(UIMenuComponent); const click = findEntity('Key Click').getComponent(AudioComponent);
fileEntity.addComponent(new UITooltipComponent({ text: 'File actions', placement: 'bottom', delay: 500 }));
on('menu:file', (event) => { if (event.eventType === EventType.UIMenu.Open) log('menu opened with', menu.getItems().length, 'entries'); });
// The entries are nested below the menu: listen on each entry's own emitter. for (const item of menu.getItems()) { item.entity.addComponent(new SignalEmitterComponent({ signal: 'menu:file-item', eventFilter: ['UIMenuItem::select'] })); } on('menu:file-item', (event) => { log('chose', event.data.value); if (event.data.value === 'quit') emit('game/quit'); });
// Every keyboard entity reports on 'ui:keyboard' by default. on('ui:keyboard', (event) => { if (event.eventType === EventType.UIKeyboard.Press) click.play(); });}A File menu with three UI Menu Items, a tooltip added by a script, and an in-scene QWERTY UI Keyboard whose key presses play a click.
Rest the pointer on File to see its tooltip, press it and choose an entry: the log names it. Click the note field and press keys on the in-scene keyboard to hear them click. Drag outside the panel to orbit the camera, scroll to zoom.
How it works:
- A UI Tooltip added to the menu trigger shows “File actions” after half a second of hover.
UIMenu::opencomes from the menu’s own Signal Emitter. Each entry sendsUIMenuItem::selectfrom its own entity, so the script gives every entry an emitter on one shared signal, filtered to that event only.- Every in-scene keyboard reports on
ui:keyboardwithout setup, so a key click sound takes three lines.
Common mistakes
Section titled “Common mistakes”- No Signal Emitter. A control without one sends nothing, and
onnever fires. Add the emitter to the control’s own entity, not to its panel (except for a UI List, whose events come from the list). - Not checking
eventType. A signal can carry several kinds of events (changeandchange-end,focusandsubmit). Compare withEventTypeconstants so each branch runs only for its event. - Feedback loops.
setValueandsetCheckedsendchangeevents. A listener that sets the same control again in response can loop; set a different control, or compare before setting. - Listening to both
UIMenuItem::selectandUIButton::presson a menu entry runs your code twice. Pick one. - Trusting a
numberinput. Thenumbertype only picks the keyboard. Parse and check the text yourself before using it. - Tooltips on touch screens. A touch has no hover, so a tooltip never appears on its own. Call
show()when the hint matters. - A list that never scrolls. Without a
heightormaxHeighton the list’s layout, the list grows to fit all its rows instead of scrolling.