This document is about: FUSION 2-SHARED
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; }).

Networked Properties CANNOT be accessed until the NetworkBehaviour has had Spawned() called.

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.

Back to top