Skip to content

Show a value in my UI

A label on your screen shows a value of the game, such as the player’s gold, and follows it when it changes.

  1. Make a TextMeshPro label on your canvas. It must be a TMP_Text.
  2. On the same object, choose Add Component > Intrigues > UI > Bound Text.
  3. Set Mode to Text. In Text, write the words with tokens in braces: {player.Gold} gold.
  4. Play. The label shows the value and follows it.

{player} is this machine’s player, so this line needs no Subject Source. {world} reads the world: {world.Date} is the date, and the World variables are members too.

For a clan, a scheme or a character, put a Subject Source on a panel above the label and write {this.Name}.

IntrigueUI.Bind takes a text with tokens and a callback that receives the formatted text. Example, copied from section 6 of UI_GUIDE.md. Compiled against the package’s runtime assembly inside a MonoBehaviour with a TMP_Text goldLabel field; not run in Unity. You need using MrW.Intrigues; and using MrW.Intrigues.UI;.

UIBinding gold = IntrigueUI.Bind("{player.Gold} gold", IntrigueUI.Local, text => goldLabel.text = text);
gold.SetText("{player.Gold} coins"); // another text through the same binding
gold.Dispose();

The callback runs at the first flush, then only when the text differs from the last one shown. IntrigueUI.Local is the view that follows the player, and Bind also takes a TextContext, an Actor or a UISubject.

The label redraws when the value changes, and when the player switches character.

In Play mode, the Bound Text Inspector shows a line, Now shows, with what the label has now.

  • The label shows the braces. A token that names nothing is printed as written, so a typo shows. The {this…} button lists the members that exist.
  • The label is empty. A value that is not there reads as empty.
  • The component shows a yellow box. Bound Text needs a TextMeshPro label on its object.
  • No Gold value exists. The line assumes a character value called Gold, so use your own name.
  • The code binding leaks. Call Dispose() in OnDisable. A binding also ends by itself when its subject leaves the world.
  • A complex text is late. A text that goes through other objects or uses {if} is formatted again at most every 0.25 seconds.