Skip to content

Variables from code

Every variable you made in the Studio can be read and written from C#. This page lists where the values live in code and which calls reach them. For what a variable is, see Variables.

  • You want to read or change a value from your own script.
  • You need to react when a value changes.
  • You fill an Actor List from code.

A holder keeps its values in a VariableSet. You reach the set, or a shortcut on the holder itself.

Scope Where the values are Source file
World IM.Variables Manager/IM.Variables.cs
Each Actor actor.Variables Actors/Actor.Variables.cs
Each Clan clan.Variables Clans/Clan.cs
Each Family family.Variables Families/Family.cs
Each Relationship actor.Relationship(target) Actors/Actor.Variables.cs
This Scheme schemeRunner.Variables Graph/SchemeRunner.cs

Without an Intrigue Manager in the scene, IM.Variables is an empty set: reads give defaults and writes are refused.

Actor, clan and family have the same four shortcuts. The World has the first three, static on IM.

Signature What it does
Variable GetVariable(string variableNameOrId) The variable, or null when it does not exist for that holder.
T GetVariable<T>(string variableNameOrId) The value as T. A missing variable gives default(T) and one warning.
bool SetVariable<T>(string variableNameOrId, T value) Sets the value. Numbers are converted and kept within the variable’s limits. False, with one warning, when the variable is missing or cannot hold the value.
bool AddVariable(string variableNameOrId, double amount) Adds to an Integer or Float. A negative amount subtracts. For the World, use IM.Variables.Add.

Sources: Manager/IM.Variables.cs, Actors/Actor.Variables.cs, Clans/Clan.cs, Families/Family.cs.

Example, not compiled. The names Marks and Year stand for variables of your database (the demo has a Marks Integer that each actor holds):

int year = IM.Variables.Get<int>("Year");
actor.AddVariable("Marks", 5);
bool done = actor.SetVariable("Marks", 40);

VariableSet is the type behind every holder. Its public calls:

Signature What it does
Variable Find(string nameOrId) and the indexer set["name"] The variable, or null. Never logs.
bool Has(string nameOrId) True when the variable exists in this set.
T Get<T>(string nameOrId) The value as T. Reads int, long, float, double, bool, string, a C# enum, Actor, Clan, Family or a Unity asset type.
bool TryGet<T>(string nameOrId, out T value) False, without a log, when the variable is missing or cannot be read as T.
bool Set<T>(string nameOrId, T value) An Enum takes an option name, a C# enum value of the same name or an option index. Actor, Clan and Family take the object, its ID or its name.
bool Add(string nameOrId, double amount) Adds to an Integer or Float.
bool Reset(string nameOrId) Puts one variable back to its default.
void ResetAll() Puts every variable back to its default; Actor Lists are emptied. Unlike a single write, it is not refused off the host of a multiplayer game.
int GetLevel(string nameOrId) The named level, or -1 without levels, below the first level, or for a missing variable.
ActorList GetActors(string nameOrId) The Actor List. Null, with one warning, when missing or not an Actor List.

Properties: Registry, Scope, Owner, Toward, Count, IsReadOnly. VariableSet also lists its variables, so you can use foreach. Source: Variables/VariableSet.cs.

Find and GetVariable give a Variable. Useful members, all from Variables/Variable.cs:

Member What it gives
Name, ID, Type, Scope, Owner What and whose it is.
Number, Int, Float, Bool, Text, Option, OptionIndex The value in the shape you ask for. Integer and Float include the bonus of traits and modifiers.
Actor, Clan, Family, Object, Actors The value of a reference, asset or Actor List variable.
Base, Bonus The stored number, and what traits and modifiers add on top.
Level, LevelNumber, LevelName, NextLevelAt The named levels of a number.
IsDefault, IsKnown Whether the value is still the default, and whether it is known on this machine.
bool Add(double amount), bool Reset(), bool TrySet(object value), bool CopyFrom(Variable other) Write calls.

To add to a number, call Add, not +=. v.Int += 1 stores base plus bonus plus 1, so the bonus of traits and modifiers is counted twice from then on. Add raises the base only.

A relationship value is held by one actor toward another. These calls are on Actor:

Signature What it does
VariableSet Relationship(Actor target) The values toward the target. Created on first use; null for a null target.
VariableSet FindRelationship(Actor target) The values when any were written or loaded, else null. Creates nothing.
Variable GetRelationVariable(string variableNameOrId, Actor target) The variable, or null.
T GetRelationVariable<T>(string variableNameOrId, Actor target) The value. A pair nobody wrote reads the defaults.
bool SetRelationVariable<T>(string variableNameOrId, T value, Actor target) Writes the value and creates the pair’s values.
IReadOnlyList<VariableSet> Relationships The relationships in use.
int GetRelationships(List<VariableSet> into) Fills your list and returns how many; allocates nothing.

GetRelationVariable for a pair nobody wrote gives a read-only handle. A write through it returns false with one warning, and its Changed event never fires. Do not keep it: ask again after a write, or write through Relationship(target).

A running scheme keeps its own variables. On a SchemeRunner:

Signature What it does
VariableSet Variables The variables of this run.
Variable GetVariable(string variableNameOrId) One of them, or null.
Actor GetActor(string variableNameOrId) The actor of an Actor variable; null when empty or missing.
bool SetActor(string variableNameOrId, Actor actor) Sets an Actor variable; null empties it.
ActorList GetActors(string variableNameOrId) An Actor List of the run.
ActorList Accomplices The built-in Accomplices list.

Source: Graph/SchemeRunner.cs. How to reach a runner from code is on Schemes from code.

An Actor List holds actors in order, each once. It is always empty at the start. ActorList is a read-only list of Actor (Count, indexer, First, Last, IsEmpty, Contains, IndexOf, CopyTo(List<Actor> into, bool includeDead = true)) with these writes. Actors that left the game are not returned and are dropped at the next write; dead actors stay until something removes them:

Signature What it does
bool Add(Actor actor) Adds at the end. False for null, an actor not in the game, or one already in the list.
bool Remove(Actor actor) False when the actor is not in the list.
bool Clear() True when the list is empty afterwards.
bool Set(IEnumerable<Actor> actors) Replaces the list in the order given; null entries, actors not in the game and repeats are left out. Set(null) empties the list.

ActorList has its own Changed event, event Action<ActorList>. Source: Variables/ActorList.cs.

Event When it fires
IM.onVariableChanged (Action<Variable>) After any value changed, in any scope.
Variable.Changed (Action<Variable>) One value changed.
VariableSet.Changed (Action<Variable>) Any value of that set changed.
ActorList.Changed (Action<ActorList>) The list really changed.

VariableSet.Changed also fires when what a value reads as changes only because a bonus changed. Each listener of IM.onVariableChanged runs in its own try / catch.

For a UI that follows a value, see IntrigueUI from code. The other events are in Events reference.

  • A missing name logs one warning and returns default(T). Use TryGet or Has to ask without a log.
  • A write that cannot hold the type returns false and logs one warning. Check the result when it matters.
  • On a machine that is not the host of a multiplayer game, a write is refused and nothing is stored. The multiplayer add-on is not covered here.