Tutorial 9: Connect your game code
What you build
Section titled “What you build”A small scheme, Raise the Alarm, that calls your own C# three times and, once the code has had its say, ends by opening a gate in your scene.
- A check asks your code whether the player’s clan has enough grown members to man the gate.
- Your code names the oldest living clan member, and the scheme keeps that person in a variable.
- An action writes a line to the Console.
- A Send Signal node tells a Signal Receiver to open the gate.
You need a scene that already runs Intrigues: an Intrigue Manager with a database, and a player character who belongs to a clan. The world from the earlier tutorials will do. The background pages are Code methods: [IntrigueMethod] and Signals.
Step 1. Write the methods
Section titled “Step 1. Write the methods”Create a script GateRules.cs in your Assets folder. Each method is static and marked [IntrigueMethod]. The return type decides which node can use it.
Example. Compiled against the package’s runtime assembly; not run in Unity.
using MrW.Intrigues;using UnityEngine;
namespace MyGame{ public static class GateRules { public const int AdultAge = 16;
// A check: true or false, or null when there is no answer. [IntrigueMethod(Description = "True when the conspirator's clan has at least three living members of 16 or older.", ReadsOnlyIntrigues = true)] public static bool? ClanCanManTheGate(Actor conspirator) { var clan = conspirator?.Clan; if (clan == null) return null;
var adults = 0; foreach (var member in clan.Members) if (member.State == Actor.LifeState.Alive && member.Age >= AdultAge) adults++; return adults >= 3; }
// A method that gives an actor back: Get From Code uses it. [IntrigueMethod(Description = "The oldest living member of the conspirator's clan, or nobody.", ReadsOnlyIntrigues = true)] public static Actor OldestOfTheClan(Actor conspirator) { var clan = conspirator?.Clan; if (clan == null) return null;
Actor oldest = null; foreach (var member in clan.Members) if (member.State == Actor.LifeState.Alive && (oldest == null || member.Age > oldest.Age)) oldest = member; return oldest; }
// An action: returns nothing, counts as yes once it has run. [IntrigueMethod(Description = "Writes a line to the Console: who sounds the horn and who answers.")] public static void SoundTheHorn(IntrigueCall call) { var elder = call.Named("Elder"); var who = call.Conspirator != null ? call.Conspirator.Name : "Nobody"; Debug.Log($"{who} sounds the horn. {(elder != null ? elder.Name : "Nobody")} answers."); } }}What the code uses, and where it comes from:
| Part | Meaning | Source |
|---|---|---|
bool? |
A check. Null is no answer. | MethodTable.cs |
Actor as the return type |
Used by Get From Code. | MethodTable.cs |
void |
An action. It counts as yes once it has run. | MethodTable.cs |
(Actor conspirator) |
The method gets the node’s For actor. | MethodTable.cs |
(IntrigueCall call) |
The method gets everything the caller knows. call.Named reads an Actor variable by its name or ID. |
IntrigueCall.cs |
ReadsOnlyIntrigues = true |
A promise that the answer depends only on Intrigues state. The two lookups do. The horn writes to the Console and does not claim it. | IntrigueMethodAttribute.cs |
Let Unity compile the script. The full lists are on Code methods: a method returns void, bool, bool?, Actor, (Actor, Actor), Clan, Family or a list of actors, and takes (), (Actor conspirator), (Actor conspirator, Actor target), (Scheme scheme) or (IntrigueCall call). Any other method is left out, and IntrigueMethods.LeftOut says why.
Step 2. Draw the scheme
Section titled “Step 2. Draw the scheme”- In the Studio, make a scheme with + in the toolbar and call it
Raise the Alarm. - In the scheme’s settings, turn on Target Not Required. The scheme will start without a target.
- In the Cast section, click + New actor variable, type
Elderand press Enter. - Add a Run Code node. Set Method to
ClanCanManTheGate. For stays on the Conspirator. - Add a Get From Code node. Set Method Name to
OldestOfTheClan. That sets Gives to An actor. Set Store In toElder. - Add a second Run Code node and set Method to
SoundTheHorn. - Add a Send Signal node. Leave its Signal empty for now.
- Link them like this:
| From | Output | To |
|---|---|---|
| Start | Start | the first Run Code |
| first Run Code | True | Get From Code |
| first Run Code | False | End |
| first Run Code | No Answer | End |
| Get From Code | Found | the second Run Code |
| Get From Code | Not Found | End |
| second Run Code | True | Send Signal |
| Send Signal | Out | End |
Link No Answer on a Run Code node. Otherwise a method that throws ends the flow at the node. The second Run Code also has False and No Answer outputs. A void method goes through True once it has run and through No Answer only if it throws. Link No Answer there too if you want the flow to go on.
A Test run does not call your methods. The Test run page says game code does not run there. For each method the setup window asks you for an answer instead. You check the real run in Play mode, in Step 5.
Step 3. Make the Signal
Section titled “Step 3. Make the Signal”In the Project window choose Create > MrW > Intrigues > Signal and name the file Gate Open. In its Description write when it is sent and what reacts. The Inspector then lists SENT BY and LISTENERS IN OPEN SCENES, both empty for now.
Back in the Studio, pick Gate Open in the Signal field of the Send Signal node. The Inspector’s SENT BY now shows that node.
Step 4. Build the gate in the scene
Section titled “Step 4. Build the gate in the scene”- Make two objects:
ClosedGateandOpenGate. SwitchOpenGateoff. - Make an empty object
Gate. Create the scriptGateDoor.csand add it toGate.
Example. Compiled against the package’s runtime assembly; not run in Unity.
using MrW.Intrigues;using UnityEngine;
namespace MyGame{ public class GateDoor : MonoBehaviour { [SerializeField] private GameObject closedGate; [SerializeField] private GameObject openGate;
// Wire this to "Do This" on a Signal Receiver reaction. public void Open() { var sent = IM.CurrentSignal; if (sent.IsSet) Debug.Log($"Signal {sent.Signal.name} from {(sent.Conspirator != null ? sent.Conspirator.Name : "code")}");
if (closedGate != null) closedGate.SetActive(false); if (openGate != null) openGate.SetActive(true); } }}IM.CurrentSignal is a SignalContext. It holds the signal, and the conspirator, target and scheme that sent it. It is set only while a signal is handled.
- Drag
ClosedGateandOpenGateinto the two fields of Gate Door. - On
Gate, choose Add Component > Intrigues > Signal Receiver. Click Add Reaction. Set Signal toGate Open. Under Do This, add an entry, dragGateinto it and pickGateDoor > Open().
A reaction is a Unity event, so you edit it the way you edit a button’s click, and the receiver listens only while its object is active and the component is enabled.
- To start the scheme, make one more script and put it on any object.
Example. Compiled against the package’s runtime assembly; not run in Unity.
using MrW.Intrigues;using UnityEngine;
namespace MyGame{ public class AlarmStarter : MonoBehaviour { [SerializeField] private string schemeName = "Raise the Alarm";
// In Play mode: the three dots of this component's header, then "Start the alarm". [ContextMenu("Start the alarm")] public void StartTheAlarm() { var player = IM.Player; if (player == null) { Debug.Log("No player is set."); return; }
var scheme = player.StartScheme(schemeName); Debug.Log(scheme != null ? "The scheme runs." : "The scheme was refused."); } }}StartScheme returns the running scheme, or null when it is refused. This is for a single-player game. On a multiplayer client the call is refused, and RequestScheme is the call to use. See Schemes from code.
Step 5. Check it works
Section titled “Step 5. Check it works”Press Play. On Alarm Starter, open the component menu (the three dots) and choose Start the alarm.
You should see, in this order:
<name> sounds the horn. <elder> answers.The first name is the player. The second is the oldest living member of the player’s clan.Signal Gate Open from <name>, andClosedGategoes off whileOpenGatecomes on.The scheme runs.It comes last: the scheme ran to its end beforeStartSchemereturned, because nothing in it waits.
In Play mode, a card of the Signal Receiver says how long ago the reaction ran. The Signal asset’s Send Now button sends the signal without the scheme, and the line from GateDoor then says from code.
To check the other paths, give the player a clan with fewer than three living adults. The first Run Code then goes through False, nothing else happens, and The scheme runs. is printed last all the same.
What can go wrong
Section titled “What can go wrong”| What you see | Why | What to do |
|---|---|---|
| A method is not in the picker. | Its parameters or return type are outside the lists in Step 1, it is an instance method of a struct or an interface, or it gives something the node does not ask for (Run Code lists only void, bool and bool? methods). | Read IntrigueMethods.LeftOut for the reason. |
The scheme was refused. |
A requirement is unmet, the scheme has no graph, or it already runs against that target. | Ask CanStart first and read its reasons. |
The horn says Nobody answers. |
Store In is not Elder, or the clan has no living member. |
Check the node; the card warns about a wrong kind. |
| The gate does not open. | The reaction has no signal, or no action, or the receiver’s object is off. | The reaction card names the mistake in plain words. |
| The gate is closed again after a load. | On Game Loaded runs only for schemes that were running at the save, and this one ends at once. | Keep the gate’s state yourself, for example in a save part as in Tutorial 11, and set the gate after a load. See After a load. |
Where to go next
Section titled “Where to go next”- Call my C# from a scheme is the short recipe for the same idea.
- Signal and Signal Receiver lists every field and message.
- Rules for code that touches Intrigues: main thread, the host, and dice.
- Next: Tutorial 10: Your own character panel.