Network Object
Overview
The NetworkObject
component assigns a network identity to a GameObject
so it can be shared across the network. Any GameObject
that is seen by all players and is a dynamic part of the scene should carry a NetworkObject
script.
Fusion offers two classes for creating custom network behaviours on a NetworkObject
:
SimulationBehaviour
for simulation related behaviours; and,NetworkBehaviour
for behaviours keeping or tracking a[Networked]
state.
NetworkObject
A single NetworkObject
script on the root node of a GameObject
is sufficient to control the entire hierarchy. At runtime the NetworkObject
will find all SimulationBehaviour
s and NetworkBehaviour
s in its hierarchy.
NetworkObject
s can be nested. In this case, the NetworkObject
will not search into nested children and let child NetworkObject
s track all SimulationBehaviour
s and NetworkBehaviour
s below it. A typical usecase would be a two player controlled tank made of a chassis and a turret where the driver controls the chassis' direction (movement) and the gunner controls the turret (shooting).
Prefab / Object Table
In order for the Prefab
parameter to make sense to Fusion (and to be sure that all clients agree on exactly which prefab that is), the prefab itself must be registered with Fusion. Under normal circumstances the Fusion toolset will automatically detect these prefabs and register them, but you can also manually trigger this detection by selecting the NetworkProjectConfig
and pressing Rebuild Object Table
in the Unity Inspector.
Instantiation / Spawn
The instantiation of NetworkObject
prefabs requires two steps:
- At edit-time: ensure the clients have to have identical
NetworkProjectConfig
assets which agrees on theNetworkObject
prefab identities. This is done by runningRebuild Object Table
in theNetworkProjectConfig
asset; and, - At runtime: call
Spawn()
on theNetworkRunner
with aNetworkObject
prefab as the parameter.
IMPORTANT: Calling the generic Unity Instantiate()
method will just create a broken local instance!
Destruction / Despawn
To destroy a NetworkObject
, call Runner.Despawn()
and pass the object as a parameter. This will destroy the object as well as all nested objects; to despawn only the top object, the nested objects need to be de-parented manually first.
Finding NetworkObject
A NetworkObject
can be found using NetworkRunner.TryFindObject()
by passing the NetworkId
that references the instance of that NetworkObject
.
NetworkId
s and NetworkObject
s can be networked with the [Networked]
attribute, but networking the NetworkObject
will only add the NetworkId
on the Game State (No data from the actual NetworkObject
will be copied to the buffer again, only the reference for it).
When accessing a [Networked] NetworkObject
, the NetworkRunner.TryFindObject()
is executed beforehand, so it is the developer choice to network the ID and do it manually or the Object itself.
State Authority
State Authority indicates who has the right to change and decide what is the correct value of a [Networked]
property.
Although the concept itself is identical for all topologies in Fusion, there are functional differences.
ServerMode and HostMode
In ServerMode
and HostMode
, the simulation on the server / host is single state authority. It is therefore possible to encapsulate code paths which should only be executed there by using an if-statement with Object.HasStateAuthority
. Object.HasStateAuthority
will only ever return true on the server / host.
In Server/Client Mode this value will always be PlayerRef.None
, as the Server/Host peer will always act as State Authority.
SharedMode
In SharedMode
, the state authority is shared between players. Every player can have state authority over a certain number of objects in the simulation. Therefore Object.HasStateAuthority
will return true for the player who has authority over it. The Photon Shared Server manages State Authority of NetworkObjects
, but does not HAVE State Authority over those objects.
The State Authority indicates which PlayerRef
's peer is the final authority on the NetworkObject
's state (Networked Properties), and its Networked Property values are replicated to other clients as Tick Snapshots.
NOTE: The StateAuthority
property is only applicable to Shared Mode.
Although the State Authority of an NetworkObject
may change in SharedMode
, there is always only one player who has it for a given object. It is not possible for multiple players to have state authority over the same object!
DestroyWhenStateAuthorityLeaves
: Indicates if thisNetworkObject
will automatically be despawned when the Player withStateAuthority
over it leaves the game.AllowStateAuthorityOverride
: Indicates if the State Authority can be acquired by a Player when another player already has the State Authority. N.B.: This property must be set before spawning, as it cannot be changed after spawn. In most use cases, leaveAllowStateAuthorityOverride
enabled for Prefabs and Scene Objects.ReleaseStateAuthority()
: The current State Authority releases control of the object.RequestStateAuthority()
: Can be called on a client to assign itsPlayerRef
as the State Authority for theNetworkObject
. The Photon Shared Server will only grant the request if:- The Object has
AllowStateAuthorityOverride
enabled; or, - The Object has no current State Authority. This can be because the State Authority left the game session or because it previously relinquished control by call
ReleaseStateAuthority()
on the object.
- The Object has
StateAuthorityChanged()
: Callback called on any implementation ofIStateAuthorityChanged
interface when the state authority over the object changes.
Input Authority
The InputAuthority
property indicates which PlayerRef
's input data (INetworkInput
) is returned when GetInput()
is called in any FixedUpdateNetwork
on this NetworkObject
. Player Input is only available to the client with Input Authority, and to the State Authority. All other clients (Proxies) will return false
when GetInput()
is called.
Input Authority and Fusion's INetworkInput
handling are primarily meant for ServerMode
and HostMode
, as they are fundamental to client prediction and resimulation. In SharedMode
however, user inputs are consumed immediately by the State Authority - making the INetworkStruct
input system non-essential. It may still be desirable to use the Fusion input system in SharedMode
for convenience as well as leaving the doors open to switching to ServerMode
or HostMode
in the future, and want to avoid refactoring later.
HasInputAuthority
: will returntrue
if theRunner.LocalPlayer == Object.InputAuthority
. Use this in scripts to test if theNetworkRunner.LocalPlayer
is the Input Authority for thisNetworkObject
.AssignInputAuthority()
: Assigns input authority to thePlayerRef
passed to the method. This can only be changed by the State Authority of the Object.RemoveInputAuthority()
: Removes input authority from theNetworkObject
by setting it toPlayerRef.None
. This can only be changed by the State Authority of the Object.