DocsBehavior

Data, events and lists

Your game and its UI agree on two things: data paths the UI reads, and event names the UI sends. That’s the whole contract. Re-export the UI as often as you like: moving, restyling or re-laying out anything leaves game code alone. What does need a code change is the contract itself: renaming an event or a data path, or changing what a payload carries.

Select a text bound to {player.hp}, then pick the Low health and Hunt start scenarios from the Data menu to see the canvas follow.

Bindings

Select a layer, open the inspector’s Interact tab and type a data path under Data bindings:

Property Example
text player.name
value, max Progress bars: player.hp, player.hpMax
visible hud.showCrit (prefix with ! to negate: !store.any)
opacity ui.fade
disabled !shop.canAfford

Text layers can also use templates: {player.hp} / {player.hpMax}. A path the data doesn’t have leaves the template visible, on purpose, so you notice. Data & events lists every data path with the layers that read it. A binding can also be an expression.

From the game:

ui.set_data("player.hp", 48)
ui.merge_data({"boss": {"hp": 22, "antlers": 2}})
ui.setData("player.hp", 48);
ui.setData({ boss: { hp: 22, antlers: 2 } });

Expressions

A text or a binding can work out what it shows. In text, write the expression in braces. In a binding field, write it without braces. With player.hp at 72 and player.hpMax at 100:

  • {player.hp / player.hpMax | percent} shows 72%.
  • {gold | compact} shows 1.5K when gold is 1500.
  • {time | duration} shows 2:05 when time is 125 seconds.
  • {lives == 1 ? "life" : "lives"} picks a word.
  • {"Lv " + level} joins text and a number.
  • {index + 1} numbers the rows of a list from one.

In a binding, player.hp < 20 as the visible binding shows a layer only while health is low, and player.gold < shop.price as disabled locks a button the player can’t afford. visible, opacity, text, value, max and disabled all take expressions. A plain path and !path work as before.

An expression can use numbers, text in single or double quotes, true, false, null, data paths, the operators + - * / %, == != < <= > >=, && || !, ? : and parentheses. It can’t call functions. Envy reads it with its own parser and never runs it as code.

Add one formatter after a |:

Formatter Shows
int The rounded number
percent The number times 100, rounded, with %
fixed:n n decimals, 0 to 12
duration Seconds as m:ss, or h:mm:ss from an hour up
compact 950, 1.5K, 2.3M, 1.2B
number Thousands groups for the language in use: a comma in English, a dot in German, Spanish and Brazilian Portuguese (pt-BR), a thin space in French
upper, lower The case of the text

There is no date formatter. Your game formats dates and sets the text as data. An unknown formatter leaves the value as it is and gives a warning.

What to know when you write one:

  • An expression needs data. If every path in it is missing, it shows as written, like a template does, and so does text in braces that doesn’t parse. Floor {1-3} and {3/5} stay text. Once one path has a value, the expression is worked out.
  • A result of null shows nothing. Dividing by zero gives null.
  • Under the text field and the binding fields, the inspector shows what the expression gives with the data on the canvas. A bad expression is marked there. In text it is a warning and the text shows as written. In a binding it is an error, so it blocks the exports until you fix it.
  • Data & events and the generated code list the data paths an expression reads, not the expression. Game code sets those paths.

Sample data and scenarios

Sample data fills the canvas and preview before the game exists. Edit it as JSON in Data & events › Sample JSON.

Scenarios are named patches over the sample data. The Last Hunt · UI kit v2 has three: Hunt start, Low health and Boss enraged. Click one in Data & events › Scenarios to see it on the canvas. A Scenario: chip above the canvas names it, and its × goes back to the sample data. In Preview, the Data scenario list picks one, and Save as scenario keeps the values that now differ from the sample data as a new scenario.

The AI handoff renders every screen in every scenario, so the programmer sees the edge cases too. Runtimes never read scenarios.

Events

A button’s click can emit an event with an optional payload. Game code listens for the name, never for a particular button:

func _on_ui_event(event: String, payload: String) -> void:
	match event:
		"new_hunt": start_new_hunt()
		"buy": shop.buy(payload)
ui.on("new_hunt", () => game.startHunt());
ui.on("*", (payload, event) => console.log(event, payload));

Data & events lists every event with the buttons that send it.

Actions

A click runs a list of actions in order, no code needed. Add them in the Interact tab under On click:

Action Stored as Does
Send event to game emit Sends an event, with an optional payload
Go to screen goto Shows another screen (with the screen’s transition, or its own)
Open screen over this one push Opens a screen as an overlay
Close overlay pop Closes the top overlay
Play animation play Plays an animation
Set data / Toggle data set / toggle Changes data, like settings.subtitles
Play sound sound Plays a sound from Assets › Sounds, at a volume from 0 to 1. See Hover, tooltips and sound

On 02 Main menu in The Last Hunt · UI kit v2, New hunt sends new_hunt and then goes to 03 Character select, so the prototype clicks through and the game still gets its event.

The same actions work under On hover and On hover end (events.enter and events.leave), which run when the pointer comes over a layer and leaves it. See Hover, tooltips and sound.

Controls

A control makes a button a working slider, select or text input. The button keeps its layers, states, focus and bindings, and also reads and writes the data at its path. In Preview and in the game it works. On the canvas it only draws its value.

Toggles and checkboxes need no control. A button with a Toggle data action and visible bindings on its check mark is one. The art is yours: a control is a button you drew, with the behavior added.

To add one, open the small menu beside the Button tool in the toolbar and pick Slider, Select or Text input, then draw it or click the screen. Envy places a button with its layers set up and gives it a data path with a sample value, so it works in Preview at once. For a button you drew, select it, open the Interact tab and set Kind under Control. Create layers adds the ones the kind needs.

Field Control Meaning
Data All The data path the control reads and writes
Min, Max, Step Slider The range and the step. 0, 100 and 1 by default
Thumb Slider The layer that moves along the track
Options (Value, Label) Select The choices, in order. A value is text or a number
Wrap around with left and right Select Left and right go round past the ends
Label Select, text input The text layer that shows the value
Empty Select, text input The text shown while there is no value
Max length Text input The longest text the player can type
Change, Commit All Event names the control sends, see below
"control": { "kind": "slider", "path": "settings.volume", "min": 0, "max": 100, "step": 5, "thumb": "<thumb layer id>", "commit": "volume_set" }

Slider. Press or drag anywhere on the button, sideways only. The value snaps to the step between the minimum and the maximum. Draw the bar with a progress layer bound to the same path and the same range, with Animate value changes off so it follows the pointer; a new slider comes with one. The Thumb follows the value along its parent. A slider doesn’t run click actions.

Select. A click, Enter, Space or gamepad A goes to the next option and goes round at the end. Left and right go either way and stop at the ends unless Wrap around is on. The Label layer shows the option’s label.

Text input. A click, Enter, Space or gamepad A starts typing in a real text field over the Label layer. Enter, or leaving the field, writes the text to the path. Escape writes nothing. While the player types, keys belong to the field, so hold your own shortcuts while ui.typing (web), is_typing() (Godot) or IsTyping (Unity) is true. There is no on-screen keyboard, and a gamepad can’t type, so a game played with a gamepad needs its own entry screen.

On a focused slider or select, left and right (keys, d-pad or stick) change the value and don’t move focus. Up, down and Tab move focus as on any button. A control with a disabled binding takes no input.

Events. Both are optional. Change is sent each time the value changes, and Commit when the change is final: when a slider is released, when a text input ends with Enter or loses focus, and at every select step. The payload is the new value. The control also writes the path, so data_changed fires, and the contract lists the path as written by the UI.

func _on_ui_event(event: String, payload: String) -> void:
	match event:
		"volume_set": audio.set_volume(float(payload))

Set a starting value from the game with set_data("settings.volume", 60). In a list the path can be an item path like item.volume, and every item sends the same event name, so the game reads the value from the item path in its data. Select options and the placeholder can be translated. On the web, a slider has role="slider" and a select role="spinbutton". Engines differ a little in what a text field can do: see known differences.

Lists

A frame can repeat one of its children for every item in an array. Select the frame and press Add in the inspector’s Repeat section:

  • Data: the array’s path, like store.items.
  • Item: the child drawn once per item, the template.
  • Key: the field that names an item (id when empty). Items keep their layers when the list is reordered.
  • Fill to and Empty: the minimum number of slots, and the child drawn for each slot the data leaves empty, like an inventory of 24 slots with 13 filled.
"list": { "path": "store.items", "template": "<row id>", "key": "id", "fill": 24, "empty": "<slot id>" }

Inside the template, item.x and {item.x} refer to the current item, and {index} is its position. An emit payload like {item.id} tells the game which item was clicked. Lists can nest.

On the canvas, the first item is the one you edit. Preview in the Repeat section shows the list with the sample data, or with 0, 1 or 100 items, to check that the frame reads well empty and full. Scenarios can set other lists for the handoff screenshots.

Updated View as Markdown