Scripting overview
What this page is for
Section titled “What this page is for”You write C# next to Intrigues when your game must start a scheme, read a clan, marry two people or react to a death. This page is the map: where the types live, how you reach the running game, who may change it, and on which thread.
When you come here
Section titled “When you come here”Come here before the first line of code that touches Intrigues. Skip it when you only draw schemes in the Studio.
Namespaces
Section titled “Namespaces”The runtime is one assembly, MrW.Intrigues, with the root namespace MrW.Intrigues. Most of what you call lives there.
| Namespace | What you find in it | Seen in |
|---|---|---|
MrW.Intrigues |
IM, IntrigueManager, Actor, Clan, Family, Role, Scheme, Succession, FamilyLaw, StartOptions |
Manager/IM.cs, Schemes/Scheme.cs, Clans/Role/Role.cs |
MrW.Intrigues.Graph |
SchemeRunner, the object that runs one scheme |
Graph/SchemeRunner.cs |
MrW.Intrigues.SaveSystem |
SaveData, the argument of IM.Load |
Manager/IM.Load.cs |
MrW.Intrigues.UI |
DialogueManager and the ready-made UI |
Dialogue/DialogueManager.cs |
Add using MrW.Intrigues; and, when you touch a running scheme, using MrW.Intrigues.Graph;.
IM is the door
Section titled “IM is the door”IM is the static face of the Intrigue Manager. It is an abstract partial class. The component you put in the scene is IntrigueManager, which derives from it and adds nothing of its own. You never create an IM.
Two members tell you whether the door is open.
| Member | What it does |
|---|---|
IM.Exists |
True when a manager is running. |
IM.Instance |
The running IntrigueManager. It throws InvalidOperationException when there is none, so test Exists first. |
Almost every static member of IM works without a manager: the lookups give an empty list, null or 0, and the writers do nothing (some log a warning first). IM.Transform and IM.GameObject are the exceptions: they read Instance and throw. IM.AddActor, for one, logs that the actor needs an Intrigue Manager in the scene and returns false. Code that runs in a scene without a manager keeps working, quietly and without effect. See The database and the Intrigue Manager for what the manager holds.
Host and single player
Section titled “Host and single player”In a single-player game your code is the host. Every call works. The word matters in a multiplayer game, where one machine hosts the world and the others hold a copy of it.
IM.IsHost is true in a single-player game and on the machine that hosts a multiplayer game. The code says that schemes, requirement checks, the dice, the day clock and every change of the world run only there.
What this means for your code:
- Reads work on every machine.
- Writers such as
Actor.StartScheme,Clan.Found,Clan.DisbandandIM.AddActorare refused off the host. A refusal logs one warning for each operation and session, and the call returnsfalse,nullor0. - A few requests are not refused.
IM.Trigger,IM.SignalandActor.Triggergo to the host when an add-on can carry them, and are refused when none can. The Multiplayer add-on carries only some of them; its Host-aware game code page says which. The clock’s speed goes to the host the same way.Actor.RequestSchemeis another request that is not refused: it asks the host and answers through your callback. Actor.RequestSchemeis the call made for this. In a single-player game or on the host it starts the scheme and runs your callback before it returns. On a client it asks the host, and the callback runs on a later frame.
A game with one player can ignore this. For a multiplayer game, read Host-aware game code first.
Example, copied from the demo project (Game/Boot/DemoFlow.cs, lines 195 to 206). It starts a tutorial scheme for every player, and only on the host.
private static void StartTutorial(){ if (!IM.Exists || !IM.IsHost) return; var players = new List<Actor>(); IM.GetPlayers(players); foreach (var actor in players) { if (actor.SchemeIsActive(TutorialScheme)) continue; if (actor.StartScheme(TutorialScheme, (Actor)null) == null) Debug.LogWarning(Title + "The \"" + TutorialScheme + "\" scheme did not start for " + actor.FullName + "."); }}The main-thread rule
Section titled “The main-thread rule”Every graph runs on the Unity main thread. Your code must call Intrigues from the main thread too.
- Requirement checks are main thread only:
Scheme.CanStart,IM.Meets,IM.CheckandActor.GetSchemesFor. There is no async form. From another thread they answer “not met” and log one error. - Save and load calls are main thread only.
- An exception thrown by a node, or by a callback that continues a flow (a delay, a dialogue click, a trigger), is logged with the node’s name. It ends that visit.
- A node that returns an unfinished task parks its flow. The continuation comes back to the main thread by itself.
Hooks and play mode
Section titled “Hooks and play mode”Static events on IM, the hooks of Succession and the hooks of FamilyLaw are cleared when a Play session starts. Subscribe from a component, or from a method marked [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] or later.
Intrigues does not survive a domain reload during play mode. Finish compiling before you press Play.
How it connects
Section titled “How it connects”- IM reference lists lookups, adding things, players, triggers, signals, language, looks, scenes and loading.
- Actor from code, Schemes from code, Families and marriage from code and Clans, roles and succession from code list the members of each part of the world.
- Rules for code that touches Intrigues collects what to avoid.
- How Intrigues fits together shows the same parts without code.
Mistakes and messages
Section titled “Mistakes and messages”| What happens | Why | What to do |
|---|---|---|
InvalidOperationException from IM.Instance |
No manager is running. | Test IM.Exists, or call a static member of IM instead. |
| A requirement check answers “not met” and logs one error | It ran off the main thread. | Call it from Update, an event handler or a coroutine. |
| One warning that an operation was refused | A writer ran on a machine that is not the host. | Run it on the host, or use Actor.RequestScheme. |
| A hook you added does nothing after Play starts | Play start cleared it. | Add it from a component, or from an AfterAssembliesLoaded method. |