Writing a custom node
What this page is for
Section titled “What this page is for”The Studio ships with its own set of nodes. When none of them does what you need, you write one. A node is a C# class. The Studio finds it, puts it in the search menu, builds its inspector from its public fields, and runs it when a flow reaches it.
If all you need is to call a method of your code from a scheme, you do not need a node. Mark the method and use Run Code; see Code methods. Write a node when you want your own card, your own outputs, or your own inspector fields.
When you come here
Section titled “When you come here”- A scheme needs a step that only your game can do, and you want it as a card with named outputs.
- You want a node a designer can drop on the canvas without knowing what code stands behind it.
- You keep a small state that must survive a save and a load.
The class
Section titled “The class”A node is a [Serializable] class that derives from NodeData. It needs a public constructor with no parameters, and its settings are plain public fields. The inspector is generated from those fields. (sources: NodeData.cs, GraphNodeAttribute.cs)
Put [GraphNode] on it:
| Part | What it does |
|---|---|
title |
The title in the node header and in the search menu. |
menu |
The search menu path, for example Time/Wait. The first segment is the root group. |
category |
A NodeCategory: Flow, Timing, Dialogue, Audio, Variable, Condition, Actor, ClanFamily, Event, Result, Utility, Mind, Players or Code. It sets the menu group and the default icon. |
The attribute also takes named settings. These are the ones the code declares:
| Setting | Default | Meaning |
|---|---|---|
Graphs |
GraphKind.Scheme |
The canvases that offer the node: Scheme, Family, Clan, or a combination. Check is not a canvas; it marks a node that is also read outside a running scheme. |
Description |
none | One sentence, shown in the inspector header and as the search tooltip. |
Icon |
category icon | A file name in the editor icon folder, or a built-in editor icon name. |
Color |
NodeColor.Auto |
The header colour. It says what the node does; Auto takes it from the category. |
HasInput |
true | False for entry nodes and nodes that never run; they get no input port. |
Creatable, Deletable, Copyable |
true | Whether the search menu offers it, the author can delete it, and copy and paste take it. |
Unique |
false | True when a graph may hold at most one. |
Order |
0 | Position among entries of the same menu group. |
AfterLoad |
Unknown |
See After a load. |
A class that derives from NodeData without [GraphNode] is logged as an error and cannot be used in the Studio. The Studio’s registry says: “Intrigues: node class X has no [GraphNode] attribute and cannot be used in the Studio.” (source: NodeRegistry.cs)
The class name is the identity of every stored node. Rename the class and the nodes of that type are lost. Pick the name once.
If your code sits in its own assembly definition, reference MrW.Intrigues. That is the name of the package’s runtime assembly (MrW.Intrigues.asmdef).
Outputs
Section titled “Outputs”By default a node has one output, called “Out”. SyncOutputs declares the outputs. The editor calls it after the node is created, after it is loaded, and after every edit.
| Member | What it does |
|---|---|
SetOutputs(params string[] names) |
Fixed outputs. Resizes the list to the names, keeps the links of outputs that remain, and names them. The output number is the index. |
SetKeyedOutputs(IEnumerable<(string key, string name)>) |
Outputs that change with the node’s settings. An output whose key survives keeps its links. A key used twice is made unique (“Spy”, then “Spy#2”). |
Both are protected methods of NodeData. Call them from SyncOutputs. (source: NodeData.cs)
Describe
Section titled “Describe”Describe(NodeDescription d) writes the summary lines on the card and the problems the Studio shows. It is pure string code: it must not use editor-only APIs or touch scene objects.
d.Line(text) adds a line. d.Error(text) and d.Warn(text) add a problem. An ActorRef writes its own words: Who.Describe(d) gives “Target” or “Target’s spouse”, and adds an error to d when nothing is chosen. (sources: NodeData.cs, ActorRef.cs)
Run(NodeInfo flow) is called once for each visit of the node, that is, each time a flow reaches it. It returns a Task. These are the rules of the code:
- Call
flow.Begin()first. - End every path with
flow.Next(i),flow.End()orflow.Stop(reason). A path that does none of them keeps the scheme alive forever. flow.Next(i)only schedules the nodes linked to outputi. They run after yourRunreturns, so code afterawait flow.Next()runs before them. Useflow.Then(action)for work that has to follow them.- Return a completed task for work done at once. Return an unfinished task, an
asyncmethod that awaits real work, and the flow waits: the node below does not start until the task completes, while the frame and other flows go on. - After you await real work, call
flow.StillValid()and return at once when it answers false. The scheme’s requirements that count while it runs were looked at by the engine before your node; this call does the same for the time you waited.
Runs happen on the main thread. An exception thrown by Run is logged with the node’s name and ends that visit. (sources: NodeData.cs, NodeInfo.cs, FlowPump.cs)
What NodeInfo gives you:
| Member | What it does |
|---|---|
Conspirator, Target |
The scheme’s two main actors. The Target may be null. |
IsCheck |
True when the node is asked outside a running scheme. |
Named(variableId) |
The actor this flow knows by an Actor variable. |
Roll() |
The visit’s dice. Call it once per visit. See Rules for code that touches Intrigues. |
Next(output, end), NextLink(output, linkIndex), Then(action) |
Move the flow on. |
End(), Stop(reason) |
End the visit. Stop also writes the reason to the Console when Verbose logging is on in the Studio settings. |
Fork(next) |
A new visit of another node that carries this visit’s context. |
Who the node acts on
Section titled “Who the node acts on”Never store an actor in a field. Use an ActorRef. It is the same picker the built-in nodes use: Conspirator, Target, a scheme actor, a specific actor, the Current Actor of a loop, and up to four relations (“Target’s spouse’s father”). Resolve(flow) returns the actor, or null when it is not available. It never throws and never falls back to another actor. See Who a node acts on.
An example
Section titled “An example”Example. Compiled against the package’s runtime assembly; not run in Unity. A node that writes an actor’s name to the Console. It is built from the signatures above, not copied from the package.
using System;using System.Threading.Tasks;using UnityEngine;using MrW.Intrigues.Graph;
[Serializable][GraphNode("Log Actor Name", "Notes/Log Actor Name", NodeCategory.Utility, Description = "Writes the name of an actor to the Console.")]public sealed class LogActorNameData : NodeData{ [Tooltip("The actor whose name is written.")] public ActorRef Who = ActorRef.Target;
public override void Describe(NodeDescription d) => d.Line($"Logs the name of {Who.Describe(d)}");
public override Task Run(NodeInfo flow) { flow.Begin(); var actor = Who.Resolve(flow); if (actor == null) { flow.Stop("The actor is not available."); return Task.CompletedTask; }
Debug.Log(actor.Name); return flow.Next(0); }}The node keeps the default single output, so it does not override SyncOutputs. Both paths end: one with Stop, one with Next.
For a node that branches, look at how the package writes one. CheckGenderData in Runtime/Graph/Nodes/Conditions.cs is short: an ActorRef, a setting, and a few overrides.
After a load
Section titled “After a load”A visit that waits while a game is saved is restored without its work in progress, and the node’s Run runs again. For most nodes that is harmless. For a node that does something before it waits, such as sending a message, the second run repeats it.
AfterLoad in [GraphNode] says which kind your node is:
| Value | Meaning |
|---|---|
Unknown |
Not said. The default; no badge. |
Safe |
Running again is safe: it does nothing before it waits, or it keeps its state. |
RunsAgain |
Running again repeats what it did before it waited. |
For RunsAgain the Studio shows a badge on the card with this tooltip: “Runs again after a load: if a game is saved while this node waits, loading it runs the node again, so it repeats what it did before the wait.” The README names Play Sound as the shipped node that shows it: its clip starts again from the beginning. (sources: GraphNodeAttribute.cs, AfterLoadBadge.cs, README.md)
Saved node state
Section titled “Saved node state”Some state cannot be rebuilt by running the node again: a count, a chosen value. Implement ISavableNode for it.
| Member | What it does |
|---|---|
StateVersion |
The version of the state your node writes, 1 or more. Raise it when the text changes its meaning. |
SaveState(NodeInfo flow) |
Returns the state to save, or null for none. Asked when a save is made while a visit of the node waits. |
RestoreState(NodeInfo flow, string state, int version) |
Gives a restored visit its state back before the run goes on. |
ISavableNode.MaxStateLength |
4096 characters. |
The state is plain text. A longer state, or a SaveState that throws, is left out of the save with a warning in the log; the visit is still saved. On a load, a state written by a newer version than the node’s is left out with an information line. A RestoreState that throws is a warning, and the visit restores without it. Do not use UnityEngine.Random in either call. One node object serves every run of a scheme, and a visit’s flow.id is numbered per run, so keep per-visit state on flow (its time, index and repeatCount) or in a table keyed by the NodeInfo, never in a field of the node. (source: ISavableNode.cs)
NodeInfo also carries a few saved fields of its own: time, index, repeatCount and indexes. Reach for them first when a number is all you need.
Optional overrides
Section titled “Optional overrides”[GraphNodePreset(menu, key)] also takes Graphs (default: scheme graphs only) and SetTo, a plain-words phrase the search tooltip uses (“Makes a Check Relation node set to Parent.”).
| Member | What it does |
|---|---|
OnCreated() |
Called once when an author creates the node. Set new-node defaults here that must differ from the field initializers; a field missing from older data keeps its initializer. |
ApplyPreset(key) with [GraphNodePreset(menu, key)] |
An extra search menu entry that makes the node and sets it up. |
RemapNodeIds(map) |
Rewrites stored references to other nodes, for example after paste. |
CanFold |
True when the Studio may fold the node into a line under its output. |
StopsSiblingFlows |
True when sibling flows after the node must not start. |
How it connects
Section titled “How it connects”- Custom conditions covers a check node with True and False outputs.
- Code methods is the lighter way to run your code from a scheme.
- How a scheme runs explains flows, outputs and the order links run in.
- Add and connect nodes shows what an author does with your node once it is there.
Mistakes and messages
Section titled “Mistakes and messages”| What you see | Why | What to do |
|---|---|---|
The node is not in the search menu, and the Console says it has no [GraphNode] attribute. |
The attribute is missing. | Add it. |
| A scheme never ends after your node. | A path of Run calls none of Next, End or Stop. |
End every path. |
| Nodes of your type vanished after a refactor. | The class was renamed. | Rename it back; the name is the stored identity. |
| A wait node repeats a message after a load. | The visit runs Run again. |
Keep what it already did with ISavableNode and skip it after RestoreState, or mark it AfterLoad = AfterLoadMode.RunsAgain so authors see the badge. |