This document is about: FUSION 2
SWITCH TO

Network Behaviour

Overview

NetworkBehaviour derives from and extends Unity’s MonoBehaviour class to include:

  • Reference to the associated NetworkRunner with the ‘Runner’ property.
  • Reference to the associated NetworkObject with the Object property.
  • Handling for Networked Properties
  • Handling for Remote Procedure Calls
  • Virtual event callbacks for Spawned(), Despawned(NetworkRunner runner, bool hasState), FixedUpdateNetwork(), and Render().
  • Aliases for authority, used for flow control. HasStateAuthority, HasInputAuthority, and IsProxy.

Any number of NetworkBehaviour components can be added to Network Object and its child transforms. Each NetworkBehaviour instance on a Network Object represents part of that Network Object’s State (Networked Properties) and Simulation (FixedUpdateNetwork()).

NetworkBehaviourId

Every NetworkBehaviour component on an attached Network Object has a unique network identifier. This identifier can itself be networked to reference a NetworkBehaviour with a Network Property or Remote Procedure Call. See Advanced Network Properties.

Networked Properties (State)

Networked Properties are properties defined in a Network Behaviour with the [Networked] attribute. These properties represent a Network Object’s State, and their values are replicated from the State Authority peer to all other interested peers. In Server Mode the Server is always the assumed State Authority. In Shared Mode, a Player is assigned as State Authority.

These properties are defined without an implementation and just an auto-implemented property (just an empty { get; set; }). The IL Weaver adds networking handling to these in the compiled IL that you don’t see.

Networked Properties CANNOT be accessed until the NetworkBehaviour has had Spawned() called, as weaved networking code references memory buffers that only exist after the Network Object has been attached to the Runner.

Event Functions

NetworkBehaviour has a number of virtual life-cycle event functions, all of which can be overridden to add implementations.

Function Description
FixedUpdateNetwork() Fusion's fixed time step callback. Called once per simulation tick. Used for simulation of core game logic.
Use in place of Unity's FixedUpdate().
Spawned() Spawned gets called after a NetworkObject is attached to the `NetworkRunner`. Called after the Network Object is initialized, and Network Properties and RPCs can be used.
Use in place of Start()
Despawned(NetworkRunner runner, bool hasState) Called before the network object is despawned.
Use in place of OnDestroy()
---
NetworkRunner runner: The NetworkRunner this NetworkObject did belong to.
bool hasState: If the state of the behaviour is still accessible.
Render() Post simulation frame rendering callback. Runs after all simulation steps (FixedUpdateNetwork) have finished.
Use in place of Update().

FixedUpdateNetwork() (Simulation)

FixedUpdateNetwork() is where your custom simulation code is implemented. You add code here that alters the current State using player input. If a NetworkObject.IsInSimulation is true, then FixedUpdateNetwork() will be called on all NetworkBehaviours of that Object every tick. The resulting State of all Network Behaviours is captured after all FixedUpdateNetwork() methods have been called, and this becomes the Object’s Snapshot for that Tick.

This sample code demonstrates a basic Health script. Heath is a Networked Property which defines the State, and code in FixedUpdateNetwork() constitutes simulation for that State.

C#

using Fusion;

public class Health : NetworkBehaviour
{
  // NetworkInput is collected elsewhere and is not covered by this section.
  public struct MyNetworkInput : INetworkInput 
  {
    public bool DrinkPotion;
  }

  // Networked Properties represent Object State.
  [Networked]
  public int Health { get; set; }

  // FixedUpdateNetwork() is your Simulation code. 
  // This is where you apply inputs to a previous Tick State,
  // to produce a new Tick State result.
  public override void FixedUpdateNetwork() 
  {
    // GetInput will return true on the StateAuthority and the InputAuthority.
    // Check to see if the player drank.
    if (GetInput<MyNetworkInput>(out var input)) 
    {
      // If the player input indicates they drank this tick, 
      // then simulate drinking. Changes to the health value 
      // on the State Authority will automatically replicate.
      if (input.DrinkPotion) 
      {
        Health += 25;
      }
    }
  }
}

Render() (Interpolation)

Because Fusion is a fixed tick-based networking engine, it produces simulation results (State Snapshots) at a fixed interval, similar to how Unity FixedUpdate() works in conjunction with Physics. However, If these snapshot results were to be rendered by just showing the latest snapshot results every Unity Update(), the results would be extremely hitchy - due to snapshots sometimes being rendered multiple updates in a row without changing, and with some snapshots being skipped and never shown. Interpolation produces smooth rendered results, by always lerping between two previously simulated snapshots.

(Animated Gif Here would be good)

Callback Interfaces

In addition to the main Spawned(), FixedUpdateNetwork() and Render() override methods, there are more specific interfaces that can be implemented for other specific timing events.

  • IAfterSpawned
  • IBeforeTick
  • IBeforeAllTicks
  • IAfterTick
  • IAfterAllTicks
  • IStateAuthorityChanged
  • IPlayerJoined
  • IPlayerLeft

Remote Procedure Calls (RPCs)

NetworkBehaviour components can declare and implement messaging handlers called Remote Procedure Calls (RPCs). For non-static RPCs, the NetworkBehaviour acts as a target for the messages. More about RPCs…

Generics

It is possible to create generic classes deriving from NetworkBehaviour. [Networked] properties and Remote Procedure Calls can be defined and implemented in a generic base class or any classes derived from that.

C#

// This is VALID
class ValidGenericClass_With_NonGenericProperty<T> : NetworkBehaviour {
    [Networked] public int Prop { get; set; }
}

However, it is NOT possible to have a generic [Networked] property of type .

C#

// This is INVALID
class InValidGenericClass_With_GenericProperty<T> : NetworkBehaviour {
    [Networked] public T Prop { get; set; }
}
Back to top