Skip to content

Start a scheme from code

Your game decides that someone plots, and your C# starts the scheme you drew in the Studio.

  1. Get the two actors: IM.Player is the local player, IM.ActorDictionary finds an actor by its ID and both belong to the main thread.
  2. Call StartScheme on the actor who should be the Conspirator. Pass the scheme’s name or ID, and the Target.
  3. Read the result. It is the running Scheme, or null when the start was refused.

Example. Compiled against the package’s runtime assembly; not run in Unity. The target ID comes from you.

using MrW.Intrigues;
using UnityEngine;
public class StartOffer : MonoBehaviour
{
public string targetId;
public void Go()
{
var conspirator = IM.Player;
if (conspirator == null || !IM.ActorDictionary.TryGetValue(targetId, out var target)) return;
Scheme started = conspirator.StartScheme("Offer a Token", target);
if (started == null) Debug.Log("The scheme was refused.");
}
}

Pass a list of actors as the third argument to start with accomplices, or StartOptions.IgnoreRequirements to skip the requirements that count to start. The demo does the second, in DemoRules.cs, for schemes its own rules decide.

actor.GetSchemesFor(target, into) fills a List<Scheme> with the schemes this actor can start against that target. Only schemes it can start are listed: one already running against the target, one a requirement refuses and one a requirement hides are left out. GetSchemesFor does not apply Target Not Required, so pass null as the target to list targetless schemes. scheme.CanStart(conspirator, target) gives a RequirementCheck with the unmet requirements and their reasons.

StartScheme changes the world, so a multiplayer client is refused. Call RequestScheme(name, target, callback) instead. On one machine and on the host, the callback runs before the call returns. On a client, it runs on a later frame.

SchemeStartResult.Started says whether it worked, and GetReasons(list) fills a List<string> with why not.

The scheme runs from its Start node and IM.onSchemeStarted fires, but a scheme can already have ended during its own start, so check started.SchemeRunner == null || started.SchemeRunner.IsEnded before you rely on it in the code that follows.

StartScheme gives null when the actor is its own target, when the scheme already runs against that target, when the name is unknown or the scheme has no graph, when the call runs on a multiplayer client, or when a requirement to start is not met, and since the call returns the same null in each of these cases, it cannot tell you which one applied.

Ask CanStart, or use RequestScheme, to read the reason.