This page is a work in progress and could be pending updates.

Execution Control

Overview

SimulationBehaviour and NetworkBehaviour derived components expose properties which can be used for conditional execution of code.

The SimulationBehaviourAttribute may also be used to conditionally disable the FixedUpdateNetwork() callback entirely for a particular Simulation Loop (Stage) or peer type (Mode).

Back To Top

Network Runner Conditionals

Simulation Loop Reference

Conditionals in relation to Simulation Loop
How the NetworkRunner conditionals relate to the Simulation Loop.

Back To Top

Simulation Stage

These properties are only valid inside of the FixedUpdateNetwork() callback, and can be used to get information about the current tick being simulated in relation to the entire Simulation Loop.

  • Runner.IsResimulation: Current tick has been simulated previously and is being simulated again now as part of client prediction reconciliation.
  • Runner.IsForward: Current tick is being simulated for the first time.
  • Runner.IsFirstTick: Current tick is the first tick of the Resimulation or Forward Loop.
  • Runner.IsLastTick: Current tick is the last tick of the Resimulation or Forward Loop.
public override void FixedUpdateNetwork()
{
  if(Runner.IsResimulation)
  {
    // execute code only if this tick has already been simulated before
  }

  if(Runner.IsForward && Runner.IsFirstTick)
  {
    // execute code only if this is the first tick of the forward simulation loop
  }
}

Note: IsFirstTick and IsLastTick may be true twice during Fusion’s Simulation Loop; once during the Resimulation Loop and once during the Forward Loop. If resimulation is possible (Clients in Server Mode), these properties must be used in conjunction with Runner.IsForward or Runner.IsResimulation to determine which loop.

Note: It is possible for a simulated tick to be both IsFirstTick and IsLastTick if there is only one tick being processed in the loop. This typically is the case for the Forward Simulation Loop.

Back To Top

Simulation Mode (Peer Type)

These properties are valid in all timing segments after Spawned() has been called.

  • Runner.IsServer: Runner represents a Server peer of any type (Dedicated, Host, or a Shared Mode plug-in).
  • Runner.IsHost: Runner represents the Server peer with a Local Player.
  • Runner.IsClient: Runner represents a Client peer with a Local Player.
  • Runner.IsPlayer: Runner represents a Player in the GameSession. True for Host and Client, False for non-Host Servers
  • Runner.IsSharedModeMasterClient - (Only valid in Shared Mode) Runner is flagged as the Master Client.
public override void FixedUpdateNetwork()
{
  if(Runner.IsServer)
  {
    // Execute code only on the game server
  }
}

Back To Top

SimulationBehaviourAttribute

An alternative to testing the above properties inside of callbacks, it is possible to conditionally exclude entire SimulationBehaviour classes from executing FixedUpdateNetwork() with the [SimulationBehaviour] attribute.

  • [SimulationBehaviour(Stages= )]: Supply a SimulationStages enum flag to specify if FixedUpdateNetwork() is to be called during the Resimulation Loop or Forward Loop. Default value of 0 is treated as Stages = SimulationStages.Resimulation | SimulationStages.Forward.
  • SimulationBehaviour(Modes= )]: Supply a SimulationModes enum flag to specify if FixedUpdateNetwork() should only be called on specific peer types. Default value of 0 is treated as all flags set.
[SimulationBehaviour(
  Stages = SimulationStages.Forward,
  Modes  = SimulationModes.Server | SimulationModes.Host
)]
public class TestNetworkBehaviour : NetworkBehaviour, IBeforeAllTicks
{
  public override FixedUpdateNetwork()
  {
    // This will only execute if Runner.IsForward and if Runner.IsServer
  }
}

Note: SimulationModes is an enum flag, and Server and Host are treated as distinct. To include ALL server types, union the two (SimulationModes.Server | SimulationModes.Host).

Back To Top

Connection Type

These members return ConnectionType (Relayed, Direct, None), which indicates if a client is connected directly to the server, or if messages are being relayed through the Photon Cloud.

  • Runner.CurrentConnectionType: If Runner is a Client, returns the type of connection to the Server. Will return ConnectionType.None if Runner is not a Client.

  • Runner.GetPlayerConnectionType(PlayerRef player): If Runner is the Server, this returns the type of connection a Client (identified by its PlayerRef) has to this Runner. Will return ConnectionType.None if Runner is not the Server.

public override void Spawned()
{
  if(Runner.CurrentConnectionType == ConnectionType.Relayed)
  {
    // Special handling for cases where messages to and from
    // this peer are being relayed through the Photon server.
  }
}

Back To Top

Game Mode (Topology)

The Runner.GameMode value can be used to selectively execute code based on the Game Session topology. In most cases this is not needed as the other conditionals described on this page are much more appropriate (such as Runner.IsServer or Runner.IsClient). There are some cases though where you may want specialized handling for particular game modes, such as Single Player.

if(Runner.GameMode == GameMode.Single)
{
  // Execute code only if in running as single player
  // Note that this value can be cached,
  // as it cannot change once the Runner has been started.
}

if(Runner.GameMode == GameMode.Shared)
{
  // Execute code only if running in shared mode.
  // Typically this is only useful for testing,
  // if you have code designed to work in both Server and Shared Modes.
}

Back To Top

Network Object Conditionals

Object Authority

  • Object.HasStateAuthority: The associated Object.Runner has State Authority over this NetworkObject.
  • Object.HasInputAuthority: The associated Object.Runner has Input Authority over this NetworkObject.
  • Object.IsProxy: The associated NetworkRunner has neither Input Authority nor State Authority over this NetworkObject. (Inputs for this object are not available on this peer).
if(Object.HasStateAuthority)
{
  // Execute only if the associated Runner has State Authority over this object
}
if(Object.HasInputAuthority)
{
  // Execute only if the associated Runner has Input Authority over this object
}
if(Object.IsProxy)
{
  // Execute only if the associated Runner has neither State Authority nor Input Authority over this object
}

Note: These properties are only valid if the associated NetworkObject is valid, as authority is per NetworkObject. If the Object is not valid, these properties will all return false without throwing exceptions or log warnings. In use cases where it is possible for the NetworkObject to be invalid and this needs to be accounted for, use Object.IsValid.

Back To Top

Other Object Conditionals

  • Object.IsValid: The associated NetworkObject is not null, and is attached to an active NetworkRunner.
  • Object.IsInSimulation: The associated NetworkObject is currently participating in its associated Simulation.
  • Object.IsSceneObject: The associated NetworkObject was attached to the network state as part of a scene, rather than having been created with a NetworkRunner.Spawn() operation.

To Document Top