Start a scheme from code
The goal
Section titled “The goal”Your game decides that someone plots, and your C# starts the scheme you drew in the Studio.
Do it, step by step
Section titled “Do it, step by step”- Get the two actors:
IM.Playeris the local player,IM.ActorDictionaryfinds an actor by its ID and both belong to the main thread. - Call
StartSchemeon the actor who should be the Conspirator. Pass the scheme’s name or ID, and the Target. - Read the result. It is the running
Scheme, ornullwhen 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.
Ask first
Section titled “Ask first”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.
Ask from a client
Section titled “Ask from a client”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.
What you will see
Section titled “What you will see”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.
What can go wrong
Section titled “What can go wrong”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.
How it connects
Section titled “How it connects”- Every member is listed on Schemes from code, the scheme itself on Schemes.
- The requirements a start asks are on Requirements.
- Code on a client is covered by Host-aware game code.
- To keep a scheme out of a list when it cannot start, see Hide a scheme the player cannot start.