Skip to content

Host-aware game code

In a multiplayer game the world changes only on the host. Players’ machines receive the changes and show them. Everything you made in the Studio already follows that rule, because schemes, rules, requirements, dialogues, signals and the characters’ own decisions run on the host. Your own code may not.

This page lists the calls that tell your code where it runs, the call to use when a player asks for a scheme, and what happens when code changes the world on the wrong machine.

All of these are public static members of IM, in IM.Players.cs except the last two, which are in IM.Replica.cs.

Signature What it does Line
bool IsHost True in a single-player game and on the machine that hosts a multiplayer game. 294
bool IsPlayer(Actor actor) True when a human plays the actor, here or on another machine. False for null and for every actor the game controls. 284
bool IsLocalPlayer(Actor actor) True when this machine’s human plays the actor. 338
bool IsRemotePlayer(Actor actor) True when a human on another machine plays the actor. 147
IReadOnlyList<Actor> Players Every played actor, the local player first. A new list on each call. 75
int GetPlayers(List<Actor> into) Fills your list with the same actors and returns the count. 89
event Action<Actor, bool> onPlayersChanged Raised on every machine when a human starts or stops playing an actor. 38
bool ReplicaReady True when this machine’s world is complete and can be read. 56
event Action onReplicaReady Raised when ReplicaReady becomes true. 64

An absent player’s actor answers false to IsPlayer, because the game plays it until the player is back.

Ask about a character, not about “the player”. IM.Player is this machine’s player only, so do not compare an actor with it. On a player’s machine, wait for ReplicaReady before your code reads the world. The Intrigue System UI does this for you. Rules for code that touches Intrigues states the same rules for single player.

Actor.RequestScheme is the call for a player who asks for a scheme (Actor.RequestScheme.cs). It has two overloads. The scheme comes as a Scheme (line 24) or as a name or ID string (line 51), followed by Actor target, Action<SchemeStartResult> callback and an optional IEnumerable<Actor> accomplices.

On the host and in single player, the scheme starts at once and the callback runs before the method returns, while on a client the request goes to the host, which checks that the player really plays that actor, asks the requirements again and answers on a later frame through your callback. If no session can carry it, you get NotSent.

SchemeStartResult (in SchemeRequests.cs) has Outcome, Started, SchemeId, Scheme, Conspirator, Target, Check and GetReasons(List<string> into, bool links = false), which lists the requirements that are not met in this machine’s language. The outcomes are Started, RequirementsNotMet, AlreadyRunning, Unknown, NotAllowed, NotStarted and NotSent. By default the host refuses a client’s list of accomplices.

Example, not compiled. It uses the signatures above.

conspirator.RequestScheme("my-scheme-id", target, result =>
{
if (result.Started) return;
var reasons = new List<string>();
result.GetReasons(reasons);
// Show the reasons to the player.
});

On a client, a call that changes the world is refused unless the host’s change is being applied. That covers an actor’s setters, roles, clans, families, marriages, births, variables, schemes, the clock, the dice and the mind’s settings. The call gives its own “not done” answer, and Intrigues logs one warning for each operation and session:

Actor.SetAge was refused: only the host may change the world. Send a command to the host instead.

Actor.SetAge stands for the operation’s name. Loading and saving, starting a scheme graph, rolling the game’s dice and picking a look are the host’s only, and their warning says “was refused on this machine: only the host does it”. (source: Authority.cs, lines 190 and 192)

With this add-on, IM.Trigger and the clock’s speed (and pause) go to the host as requests, and the host refuses them by default (HostSettings.AllowClientTriggers and AllowClientSpeed are off). Actor.Trigger and IM.Signal are not sent from a player’s machine at all; each logs one warning. Opinions are the host’s too. Mind.Opinion answers 0 on a client, so ask with MultiplayerSession.AskOpinionOfMe(Actor of, Action<OpinionOfMeAnswer> answer).

A value a player may not see is not sent, so Variable.IsKnown is false for it on that machine and it reads as its default. Who sees what covers the settings. Code you call from a requirement row with [IntrigueMethod] runs on the host. When it answers for a remote player it sees only the schemes that player may see, through Actor.SchemeIsActive, Actor.GetScheme and the running-scheme count. Code that reads Schemes, SchemesAgainstToActor or SchemesAsAccomplice directly sees every scheme.

  1. Find code that changes the world and may run on a player’s machine.
  2. Guard it with if (!IM.IsHost) return;, or move it to the host.
  3. Where a player asks for a scheme, call RequestScheme and use the callback.
  4. Replace IM.Player comparisons with IsPlayer or IsLocalPlayer.
  5. Play with two machines and read the console for “was refused” warnings.

Next: Your own messages, Scripting overview and Saving in multiplayer.