IntrigueUI from code
What this page is for
Section titled “What this page is for”IntrigueUI is the one door between the game world and your UI code. It knows nothing about uGUI, UI Toolkit or IMGUI, so any UI can build on it. You ask it to tell you when to redraw something, to keep a text current, to give you a live list, or to do something for the player. Main thread only.
When you come here
Section titled “When you come here”- You build your own HUD or menu and want it to follow the game.
- You need a button to start a scheme, change the speed or open a screen, with a reason when it is refused.
- You want a toast, a list of relatives or a save slot list.
The examples need using MrW.Intrigues; and using MrW.Intrigues.UI;.
The rules
Section titled “The rules”- A callback runs once at the first flush after you made it, then at most once per frame, and only when something it shows may have changed. It never runs inside the change itself. A flush runs in
LateUpdate. - Order in a flush: notice listeners, lists, texts and conditions, watch callbacks, then
IntrigueUI.Updated. - Dispose what you make. Watches, bindings, lists and listeners have
Dispose(). Call it inOnDisablefor a component. Calling it twice, or inside the callback, is safe. - A watch, binding or list ends by itself when its subject leaves the world. Its callback runs one last time and
IsGoneis true. Ones made forIntrigueUI.Localfollow the player and never end. - An exception in a callback is logged, and the others go on.
| Member | What it is |
|---|---|
IntrigueView Local |
This machine’s player and language. Overloads that take a view follow a player switch. |
float RecheckSeconds |
How often a text that reads something no event reports is looked at again. Default 0.25; 0 is every frame. |
float BudgetMilliseconds |
Milliseconds per frame such texts may take. Default 1. |
event Action Updated |
Raised after each flush that called anything back. |
void Flush() |
Runs a flush now, for an editor tool or a test. |
“Tell me when to redraw this.” One overload for each subject, with a mask that names what you care about.
| Signature | Watches |
|---|---|
Watch(Actor actor, ActorChange mask, Action<Actor, ActorChange> changed) |
An actor. Last call carries ActorChange.Removed. |
Watch(IntrigueView view, ActorChange mask, Action<Actor, ActorChange> changed) |
The view’s actor, whoever the player is now. |
Watch(Clan clan, GroupChange mask, Action<Clan, GroupChange> changed) |
A clan. Last call carries GroupChange.Lifecycle. |
Watch(Family family, GroupChange mask, Action<Family, GroupChange> changed) |
A family. |
Watch(Scheme scheme, SchemeChange mask, Action<Scheme, SchemeChange> changed) |
A scheme run. Last call carries SchemeChange.Ended. |
Watch(Variable variable, Action<Variable> changed) |
One value. |
WatchWorld(WorldChange mask, Action<WorldChange> changed) |
The calendar, the players, the language, the world becoming ready, a load. |
A UIWatch has IsAlive, IsGone, Refresh() and Dispose(). A null callback throws ArgumentNullException. Example, lines from UI_GUIDE.md (section 6):
UIWatch watch = IntrigueUI.Watch(IntrigueUI.Local, ActorChange.Name | ActorChange.Age, (actor, change) =>{ nameLabel.text = actor != null ? actor.Name + ", " + actor.Age : "";});UIWatch clock = IntrigueUI.WatchWorld(WorldChange.Day | WorldChange.Speed, change => Debug.Log("The calendar moved: " + change));watch.Dispose();“Keep this text current.” Bind takes a text with tokens and a callback that gets the formatted text, first at the next flush, then only when the text differs from the last one. BindFlag does the same for a condition and gives a bool.
| Signature | Subject of the text |
|---|---|
Bind(string text, TextContext context, Action<string> shown) |
A context you build. |
Bind(string text, Actor subject, Action<string> shown) |
An actor: its {target} and {this}. |
Bind(string text, UISubject subject, Action<string> shown) |
A subject of any kind, read by {this...}. |
Bind(string text, IntrigueView view, Action<string> shown) |
The player’s actor; follows the player. |
BindFlag(string condition, ...) |
The same four, with Action<bool> shown. |
A UIBinding has Text, Flag, Written, IsAlive, IsGone, IsCondition, Refresh(), SetText(string), SetContext(...) and Dispose(). Example, lines from UI_GUIDE.md (section 6):
UIBinding gold = IntrigueUI.Bind("{player.Gold} gold", IntrigueUI.Local, text => goldLabel.text = text);UIBinding adult = IntrigueUI.BindFlag("this.Age >= 18", someone, yes => adultStamp.SetActive(yes));gold.SetText("{player.Gold} coins"); // another text through the same bindinggold.Dispose();IntrigueUI.Lists makes a UIList<T>. Its callback runs at the first flush, then only when the rows or their order really changed. A row is the real object (Actor, Clan, Scheme) or a small row object reused while it stands for the same thing, so keep one row view per row and compare by reference.
Factories on IntrigueUI.Lists, by what they list:
| Subject | Factories |
|---|---|
| People and groups | Relatives, Members, Court, SeatHolders, Policies, Traits, Variables, WorldVariables, AllActors, AllClans, AllFamilies, Subjects, Realm, ClanRelations |
| Schemes | SchemeOffers, SchemeOfferSections, SectionOffers, RunningSchemes, Accomplices, AccompliceCandidates, SchemeLog |
| Mind | OpinionLines, ClanOpinionLines, Memories, Ties, Ambitions, AmbitionsOffered, Succession |
| Records | Chronicle, DecisionLog, Notices, Flags, SaveSlots |
Those about an actor also take an IntrigueView. A UIList<T> has Count, the indexer, TotalCount, WindowStart, IndexOf, CopyTo, SetWindow(first, count), Refresh(), IsAlive, IsGone and Dispose(). Example, lines from UI_GUIDE.md (section 6):
UIList<Actor> children = IntrigueUI.Lists.Relatives(IntrigueUI.Local, RelativeGroups.Child, list => Debug.Log(list.Count + " children"));UIList<Actor> helpers = IntrigueUI.Lists.Accomplices(runningScheme, list => Debug.Log(list.Count + " helpers"));Commands
Section titled “Commands”IntrigueUI.Commands is the one checked door for what a player does. Each command answers at once with a CommandResult and never throws. Each has a Can... twin that answers without doing it, so you can grey a button and show why.
A CommandResult has Outcome (a CommandOutcome: None, Done, Sent to the host of a multiplayer game, or Refused), Ok, and Reason, a plain sentence that is empty unless refused.
| Group | Commands |
|---|---|
| Schemes | StartScheme(scheme, conspirator, target, answer = null), and an overload with accomplices; Trigger(scheme, name, value = true); Signal(signal) |
| Clock | SetSpeed(ClockSpeed), Pause(), Resume(), TogglePause(), Faster(), Slower() |
| World | ChangeLanguage(code), PlayAs(actor) |
| Screens | Open(subject, source = null), OpenScreen(name, subject = default, source = null), Back(), CloseScreens() |
| Dialogues | Answer(dialogue, choice), SkipDialogue(dialogue = null), CloseDialogue(dialogue) |
| Flags | OpenFlag(flag), DismissFlag(flag) |
| Saves | SaveGame, SaveGameWith, Autosave, LoadGame, DeleteSave |
Open, Back and CloseScreens raise IntrigueUI.OpenRequested, BackRequested and CloseRequested. OpenScreen raises IntrigueUI.ScreenRequested (the name, the subject, the source). A Subject Window listens, and OpenScreen is refused when no enabled window has that screen name. Example, lines from UI_GUIDE.md (section 6):
CommandResult r = IntrigueUI.Commands.Faster();if (r.Outcome == CommandOutcome.Refused) Debug.Log(r.Reason);if (IntrigueUI.Commands.CanSetSpeed(ClockSpeed.Fastest).Ok) IntrigueUI.Commands.SetSpeed(ClockSpeed.Fastest);IntrigueUI.Commands.TogglePause();The save commands end later: the command answers when it was accepted, and the answer callback gets a SlotAnswer (Result, SlotId, Note, Deferred, Ok) when it ended. IntrigueUI.Saves has IsWorking, Note, Problem, Refresh() and the events Changed and Preparing.
Notices
Section titled “Notices”A notice is one record for “something happened”, delivered once each, in order, for toasts and logs. A feed nobody listens to costs nothing.
| Signature | What it does |
|---|---|
IntrigueUI.Notices.Listen(WorldNoticeKinds kinds, Action<WorldNotice> handler) |
Returns a NoticeListener; call Dispose() to stop. |
IntrigueUI.Notify(string text, Actor about = null) |
Puts your own line into the feed as a Custom notice, on this machine only. Nothing happens for an empty text, or while nobody listens to Custom notices. |
IntrigueUI.Lists.Notices(changed, kinds = News, about = null, seconds = 6f, max = 5, newestFirst = true) |
The latest notices as rows, for a toast stack. |
Dialogues and selection
Section titled “Dialogues and selection”| Member | What it does |
|---|---|
IntrigueUI.Dialogues.Use(IDialogueView view) |
Draws every dialogue yourself. See Your own dialogue window. |
IntrigueUI.Dialogues.View, Open, Top |
The installed view, the open dialogues and the top one. |
UISubject Selected |
What this machine’s player selected; nothing when nothing is. Clears itself when the thing leaves the world. |
void Select(UISubject subject) |
Selects it. A subject that is gone selects nothing. An actor, clan, family or scheme converts by itself. |
event Action<UISubject> SelectionChanged |
The selection changed. Not raised when a world clear dropped it. |
Mistakes and messages
Section titled “Mistakes and messages”- Calling these off the main thread.
Notices.ListenthrowsInvalidOperationExceptionthere. - Keeping a watch or list and never disposing it. Dispose in
OnDisable. - Reading
Selectedafter a load without asking again. Read it again after a load. - Expecting a callback inside the change. It comes at the next flush.
How it connects
Section titled “How it connects”- Your own dialogue window and Dialogues and announcements from code cover the dialogue side.
- Variables from code gives the values a watch or binding follows.
- Events reference lists the events behind the change notifications.
- Texts, tokens and pacing explains the tokens a binding uses.