Skip to content

Code methods: [IntrigueMethod]

A code method is a method of your own scripts that a scheme can call. You mark it with [IntrigueMethod]. The node Run Code calls the ones that do something or check something. Get From Code calls the ones that give back an actor, a pair, a clan or a family. A method that gives a group of actors is used by Among: From a method in code. This page shows what the method may return, what it may take, who answers when it is not static, and why a marked method can be left out.

  • A rule is too awkward to draw as nodes and you want to write it in C#.
  • You marked a method and the Studio does not list it.
  • You need to know what a method gets from the scheme that calls it.
[IntrigueMethod(string name = null)]
Member What it does
Name The name graphs store. Empty or null means the method’s own name. Names are unique across all methods.
Description One sentence for designers, shown with the method in the editor’s pickers.
ReadsOnlyIntrigues A promise, default false. See below.

The attribute derives from Unity’s PreserveAttribute, so code stripping keeps the marked method in a player build. It is for methods only, and is not inherited. Source: Methods/IntrigueMethodAttribute.cs.

The return type decides what the method is. MethodKind names the six kinds.

Return type MethodKind Used by
void Check An action. Counts as yes once it has run.
bool Check A check: true or false.
bool? Check A check where null means no answer.
Actor, or a class derived from it GetActor Get From Code
(Actor, Actor) GetActorPair Get From Code
Clan GetClan Get From Code
Family GetFamily Get From Code
A list, an array or another class that is an IEnumerable<Actor> GetActors A group of actors, for example Among: From a method in code

A struct that is an IEnumerable<Actor> is left out. Return a list, an array or another class.

A method cannot return a number or take a clan. The demo works around it: it leaves a number in a World variable and answers true when the number is above 0. Read the variable straight after Run Code. Source: Methods/MethodTable.cs (MethodKind, Classify) and the comment of D:Game/Rules/DemoMethods.cs.

MethodParameters names the five parameter lists. Nothing else is accepted. Each parameter must be exactly Actor, Scheme or IntrigueCall. A subclass of Actor as a parameter type is left out. The parameter names are yours.

Parameter list MethodParameters What you get
() None Nothing.
(Actor conspirator) Conspirator The first actor, the node’s For actor.
(Actor conspirator, Actor target) ConspiratorAndTarget For and Against actors.
(Scheme scheme) Scheme The running scheme. The method can only be called where a scheme runs.
(IntrigueCall call) Call Everything the caller knows. Pass it by value, not ref.

IntrigueCall is a read-only struct. Its members:

Member What it is
Conspirator, Target The two actors; either may be null.
Scheme, HasScheme The running scheme; null where no scheme runs.
Candidate The actor a Where list is checking; null outside one.
Current The actor of the For Each Actor turn; null outside one.
Accomplices The accomplices. Never null.
Named(string name) The actor stored under a variable name on this flow or in the scheme; null when none.
IntrigueCall(Actor conspirator, Actor target = null, Scheme scheme = null, Actor candidate = null, Actor current = null, IReadOnlyList<Actor> accomplices = null) A call you make from your own code.

Source: Methods/IntrigueCall.cs.

A static method needs no object in the scene. An instance method needs an object to answer, found in this order:

  1. The object you gave to IntrigueMethods.Register(object host). It can be any class: a plain C# service, a ScriptableObject, one of several components. A second object of the same class replaces the first.
  2. Otherwise, for a MonoBehaviour class, the one active and enabled object of that class in the loaded scenes.

An instance method of a class that is not a MonoBehaviour answers only after Register. If no object or several are found, the scene is searched again when a scene loads or unloads, after Register, Unregister or RefreshHosts, and otherwise at most once every 30 frames. Calls run on the main thread. Source: Methods/IntrigueMethods.cs.

Set ReadsOnlyIntrigues = true when the answer depends only on Intrigues state: actors, variables, clans, families, roles, schemes, the day. A requirement that calls such a method while a scheme runs is then looked at before a node only when something in the game changed, instead of before every node.

If the promise is false, because the method reads other game state, a clock or physics, a requirement that depends on that state is noticed late. A node can run after it broke, until something in Intrigues changes. Leave it off when you are not sure.

The demo’s DemoMethods.cs shows the shapes. These are attribute lines and signatures from that file, bodies left out:

[IntrigueMethod(Description = "The living head of the highest rank in the world (the high king when there is one), or nobody.", ReadsOnlyIntrigues = true)]
public static Actor HighestRuler()
[IntrigueMethod(Description = "The living man-at-arms of the conspirator's clan with the most Arms (knights and captains first), or nobody.", ReadsOnlyIntrigues = true)]
public static Actor StrongestKnight(Actor conspirator)
[IntrigueMethod(Description = "Whether the realms of the two actors touch: a province of one borders a province of the other. No answer without a map.", ReadsOnlyIntrigues = true)]
public static bool? IsNeighbourRealm(Actor conspirator, Actor target)
[IntrigueMethod(Description = "Rolls how hard the plague bites this month (0 to 30) and leaves it in the World variable \"Rules Result\". Kills nobody. True when above 0.")]
public static bool PlagueToll(Scheme scheme)
[IntrigueMethod(Description = "Moves the Prosperity of the provinces of the conspirator's realm (all when it has none) half way toward the World variable \"Last Harvest\". No answer without a map.")]
public static bool? ApplyHarvest(IntrigueCall call)

Example, not compiled. A method of your own, built from the rules above (the class and the name are yours to choose):

public static class MyMethods {
[IntrigueMethod(Description = "True when the conspirator is older than the target.", ReadsOnlyIntrigues = true)]
public static bool? IsOlder(Actor conspirator, Actor target) =>
conspirator == null || target == null ? (bool?)null : conspirator.Age > target.Age;
}

Actor.Age is a public int (Actors/Actor.State.cs).

IntrigueMethods.LeftOut lists marked methods that graphs cannot call, each with its reason. Classify in MethodTable.cs gives these:

The method… Reason text starts
Is not in a class is not in a class
Is generic, or in a generic class is generic, is in the generic class ...
Is an instance method of a struct or interface is an instance method of the struct / interface ...; make it static or move it to a class
Takes IntrigueCall by reference takes IntrigueCall by reference; pass it by value
Takes any other parameter list takes (...); a method graphs call takes (), (Actor conspirator), (Actor conspirator, Actor target), (Scheme scheme) or (IntrigueCall call)
Returns another type returns ...; a method graphs call returns void, bool, bool?, Actor, (Actor, Actor), Clan, Family or a list of actors (IEnumerable<Actor>)
Returns a struct that is an IEnumerable<Actor> returns a struct (...); return a list, an array or another class

Two methods with one name are a conflict. Graphs call neither until one is renamed. IntrigueMethods.Conflicts lists them, and LeftOutSameName lists left-out methods that share a name with a callable one (graphs call the callable one).

Signature What it does
IReadOnlyList<IntrigueMethodInfo> All The callable methods, sorted by name.
IntrigueMethodInfo Find(string name) The callable method, or null (unknown, left out or in a conflict).
bool TryFind(string name, out IntrigueMethodInfo info) The same with a bool.
bool? Check(string name, in IntrigueCall call) Calls a check or action. Null is no answer.
bool? Run(string name, in IntrigueCall call) The same as Check.
Actor GetActor(string name, in IntrigueCall call) Null when it gives none or cannot be called.
(Actor, Actor) GetActorPair(string name, in IntrigueCall call) (null, null) when it cannot be called.
Clan GetClan(string name, in IntrigueCall call), Family GetFamily(string name, in IntrigueCall call) Null when it cannot be called.
IReadOnlyList<Actor> GetActors(string name, in IntrigueCall call) The group as the method gave it; empty when it gave none or null; null when it cannot be called or threw.
void Register(object host), void Unregister(object host), void RefreshHosts() Hosts for instance methods.

All of these are static on IntrigueMethods. A call that gets no answer logs one warning per method and cause in a session. A method that throws is logged with its name, and the answer is no answer.

IntrigueMethodInfo carries Name, Description, ReadsOnlyIntrigues, Kind, Takes, NeedsScheme, Parameters, IsStatic, HostType, NeedsRegister, EditorOnly and Signature.

  • A method that needs a scheme ((Scheme scheme)) called where none runs gives no answer and one warning.
  • Do not call a method that writes from a requirement row. The demo’s own comment gives the same advice.
  • Link the No Answer output of Run Code, so a broken method does not end the flow there.