Skip to content

Behaviour from code

Characters nobody plays think for themselves; Characters that think explains how. Your code can listen to that thinking, ask it questions, nudge it, and stay out of its way. All of it is in the static class Mind and in a few events on IM, both in the namespace MrW.Intrigues.

Everything here runs on the main thread. The mind runs where the game’s day runs: in a single-player game and on the host of a multiplayer game. The events below are raised there. (source: IM.Mind.cs)

  • Your UI or log should show what a character just decided.
  • A scene event should make one character think now.
  • Your game must stop some decisions.
  • You keep your own state, and the editor’s Simulation plays days on behind your back.

Each listener runs in its own try and catch, so one that throws does not stop the others.

Event Payload When
IM.onDecisionMade DecisionInfo For every decision a character took on their own or by Think Now: carried out, refused, or stopped by Mind.CanDecide. Not when nothing was wanted.
IM.onMemoryChanged MemoryInfo A memory was remembered, remembered again, forgotten, ended, or went with a character that left the game.
IM.onTieChanged TieInfo A tie was added, added again, removed, ended, or went with a character.
IM.onAmbitionChanged AmbitionInfo A character’s ambitions changed: taken up, reached, given up, set or cleared by a graph or code.
IM.onAttitudeChanged AttitudeInfo The attitude one clan holds toward another changed. Not raised for the starting attitudes of a new game or for a load.
IM.onSchemeGivenUp GiveUpInfo A running scheme is given up, before the scheme hears “Abandoned” and ends. Mind.onSchemeGivenUp is the same event.

DecisionInfo has Actor, Decision, Target, Wanted, Roll, Day, Outcome and Scheme; for a clan’s decision also Clan and TargetClan. Actor is null for a clan’s decision that left the clan with nobody to speak for it. Outcome is a DecisionOutcome: Done, Refused, Vetoed, NothingToDo, GaveUp, WouldDo or WouldGiveUp. (sources: DecisionInfo.cs, IM.Mind.cs)

Member What it does
Mind.ThinkNow(actor, decisionNameOrId = null, skipDice = false, alsoForPlayers = false) Makes the actor think about one decision, or about every decision when you pass null, right now. It checks who considers it, rests, schemes at once, the scheme’s requirements to start and CanDecide. Of several decisions, the most wanted is taken. It leaves a character a player plays alone unless alsoForPlayers is set or the decision’s own setting says otherwise. It works while the world’s thinking is off. Returns a DecisionInfo.
Mind.Evaluate(actor, decisionNameOrId, target = null) How much the actor wants the decision now, in percent. No roll. Allocation-free.
Mind.Rank(actor, decisionNameOrId, results) Every character the actor would look at for the decision, weighed and sorted best first, into a List<DecisionOption>. Returns the count.
Mind.Explain(actor, decisionNameOrId, target, lines) Why the actor wants it as much as they do, one part per line. Allocates text.
Mind.Opinion(of, about) What one character thinks of another. 0 on a multiplayer client, where opinions are not known.
Mind.GiveUp(scheme, reasonText = null) Gives up a running scheme the way the review does. False for a scheme that is not running.
Mind.SetHelp(variableNameOrId, amount) Adds an amount to an Each Actor Integer or Float variable of every character no player plays. 0 removes it. False when no such variable exists.
Mind.ClearHelp() Removes every help row.
Mind.Register(name, Func<Actor, Actor, float> amount) Gives an amount “decided in code” its method. Asked with the character and the one it is about (may be null). The method must be quick and change nothing. A second method for a name replaces the first.
Mind.Unregister(name) Removes it.
event Mind.CanDecide (Func<Actor, Decision, Actor, bool>) Asked before a character carries out a decision. Any listener that answers false stops it: the outcome is Vetoed, no rest is taken, and the decision may be taken on its next day. A listener that throws counts as no objection. Listeners must be quick and change nothing.

The settings of the running game are properties of Mind too: Thinking, Activity (clamped to 0.25 to 4), Hostility (0 to 3), TowardPlayersHostile, TowardPlayersFriendly, Predictable, SchemesAtOnce and DecisionsPerFrame. They are saved. On a multiplayer client a setter is refused with one warning. (sources: MindDecisions.cs, Mind.cs, MindSocial.cs)

The Think Now node does the same thing from a scheme: see Think Now.

Example. Compiled against the package’s runtime assembly; not run in Unity. Log every decision a character carried out, and veto a decision named in a field of your own:

using MrW.Intrigues;
using UnityEngine;
public class DecisionLog : MonoBehaviour
{
private void OnEnable()
{
IM.onDecisionMade += OnDecision;
Mind.CanDecide += CanDecide;
}
private void OnDisable()
{
IM.onDecisionMade -= OnDecision;
Mind.CanDecide -= CanDecide;
}
private static void OnDecision(DecisionInfo info)
{
if (info.Outcome == DecisionOutcome.Done && info.Actor != null)
Debug.Log($"{info.Actor.Name} decided on day {info.Day}.");
}
private static bool CanDecide(Actor actor, Decision decision, Actor target)
{
return true; // Return false to stop this decision.
}
}

The editor’s Simulation plays the game on to see what would happen, then puts the game back. It is on the Studio tab Behaviour ▸ Simulation, and the window Tools ▸ MrW ▸ Intrigues ▸ Simulation opens the same tool. Simulate and Repeat a scheme play the game and run only in Play mode; Forecast reads the opening world and plays nothing.

The scene’s objects are put back by the editor afterwards. State the editor cannot see is not. Mind.Simulating is true from the moment a simulation starts until the game was put back. It is always false in a player build and in a normal game. (source: Mind.cs)

Member What it does
Mind.Simulating True while a simulation runs.
event Mind.SimulationStarted Raised when a simulation starts, before anything of it runs. Simulating is already true.
event Mind.SimulationEnded(bool putBack) Raised when it ended. putBack is true when the game was put back as it was at the start, false when it was left as the simulation ended it.

The documentation names what to do in these:

  • Code that writes files, PlayerPrefs or assets, or keeps state Unity does not serialize (statics, private fields, dictionaries), should ask Mind.Simulating, or keep its state in SimulationStarted and restore it in SimulationEnded.
  • Coroutines, Invoke calls, timers and async work started during a simulation go on after it. Cancel them in SimulationEnded.
  • Your own fields on subclasses of the Intrigue Manager or of actors are not set back.

Example. Compiled against the package’s runtime assembly; not run in Unity.

using MrW.Intrigues;
using UnityEngine;
public class MyCounter : MonoBehaviour
{
private int kept;
public int Count;
private void OnEnable()
{
Mind.SimulationStarted += Keep;
Mind.SimulationEnded += Restore;
}
private void OnDisable()
{
Mind.SimulationStarted -= Keep;
Mind.SimulationEnded -= Restore;
}
private void Keep() => kept = Count;
private void Restore(bool putBack)
{
if (putBack) Count = kept;
}
}

Mind.WarmUpAtStart is true by default and set again at the start of every Play session. The mind then pays its first-use cost when the game starts, so the first in-game day is as fast as the others. Set it to false before the first game starts and call Mind.WarmUp() behind your own loading screen instead, or not at all. The rehearsal needs a running game with decisions and at least two characters, and it calls none of your methods. (source: MindWarmUp.cs)

What you see Why What to do
ThinkNow answers NothingToDo. The decision does not exist, nothing was wanted, or the mind does not run on this machine. Check the name; call it on the host.
A decision is Vetoed and you do not know by whom. A CanDecide listener answered false. Look for listeners that subscribed.
A setting did not change on a client. Setters are refused on a client with one warning. Change it on the host.
Your counter is wrong after a simulation. State outside Unity’s serialization is not set back. Keep and restore it in SimulationStarted and SimulationEnded.