Skip to content

Read Intrigues values from my code

Your script reads a value you made in the Studio, such as a character’s gold or the world’s year, and reacts when it changes.

  1. Pick the holder. Each scope keeps its values in one place:
Scope Where you read it
World IM.GetVariable<T>(name) or IM.Variables
Actor actor.GetVariable<T>(name) or actor.Variables
Clan clan.GetVariable<T>(name)
Family family.GetVariable<T>(name)
  1. Read the value with GetVariable<T>(name). GetVariable(name) without a type gives the Variable itself, or null when it does not exist for that holder.
  2. To ask without a log, use the set: IM.Variables.TryGet<T>(name, out value) is false when the variable is missing or cannot be read as T. Has(name) asks whether it exists.
  3. To hear a change, subscribe to Variable.Changed for one value, VariableSet.Changed for every value of one holder, or IM.onVariableChanged for any value of any scope, and remember to unsubscribe again when your object goes away.

Example. Compiled against the package’s runtime assembly; not run in Unity. Gold stands for an Integer variable that each actor has in your database.

using MrW.Intrigues;
using UnityEngine;
public class GoldWatcher : MonoBehaviour
{
Variable gold;
void OnEnable()
{
var actor = IM.Player;
if (actor == null) return;
Debug.Log("Gold now: " + actor.GetVariable<int>("Gold"));
gold = actor.GetVariable("Gold");
if (gold != null) gold.Changed += OnGold;
}
void OnDisable()
{
if (gold != null) gold.Changed -= OnGold;
}
void OnGold(Variable v) => Debug.Log(v.Name + " is now " + v.Int);
}

To change a number, use AddVariable(name, amount) or Variable.Add(amount), not += on Int. The += would count the bonus of traits and modifiers twice from then on.

The value comes back as T.

For an Integer or Float, Int, Float and Number include the bonus of traits and modifiers, while Base holds the number you stored. A listener that throws is logged and does not stop the write. On Variable.Changed and VariableSet.Changed it does stop the listeners after it on the same event; each listener of IM.onVariableChanged runs in its own try and catch.

  • A missing name. GetVariable<T> logs one warning and gives default(T). Use TryGet or Has to ask without a log.
  • No Intrigue Manager in the scene. IM.Variables is an empty set. Reads give defaults, and writes do nothing.
  • A write does nothing. On a multiplayer client, the game refuses a write with one warning and stores nothing. Write on the host.
  • A relationship value nobody wrote. GetRelationVariable gives a read-only handle, and its Changed never fires. Ask again after a write.