Read Intrigues values from my code
The goal
Section titled “The goal”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.
Do it, step by step
Section titled “Do it, step by step”- 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) |
- Read the value with
GetVariable<T>(name).GetVariable(name)without a type gives theVariableitself, ornullwhen it does not exist for that holder. - 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 asT.Has(name)asks whether it exists. - To hear a change, subscribe to
Variable.Changedfor one value,VariableSet.Changedfor every value of one holder, orIM.onVariableChangedfor 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.
What you will see
Section titled “What you will see”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.
What can go wrong
Section titled “What can go wrong”- A missing name.
GetVariable<T>logs one warning and givesdefault(T). UseTryGetorHasto ask without a log. - No Intrigue Manager in the scene.
IM.Variablesis 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.
GetRelationVariablegives a read-only handle, and itsChangednever fires. Ask again after a write.
How it connects
Section titled “How it connects”- Every call and event is listed on Variables from code. What a variable is, its scopes and levels are on Variables.
- To show a value on screen without polling, see Show a value in my UI.
- Rules for code that touches Intrigues are on Rules for code that touches Intrigues.