- Finding NetworkObject
- State Authority
- Input Authority
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
Fusion offers two classes for creating custom network behaviours on a
SimulationBehaviourfor simulation related behaviours; and,
NetworkBehaviourfor behaviours keeping or tracking a
NetworkObject script on the root node of a
GameObject is sufficient to control the entire hierarchy. At runtime the
NetworkObject will find all
NetworkBehaviours in its hierarchy.
NetworkObjects can be nested. In this case, the
NetworkObject will not search into nested children and let child
NetworkObjects track all
NetworkBehaviours 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
NetworkProjectConfigassets which agrees on the
NetworkObjectprefab identities. This is done by running
Rebuild Object Tablein the
- At runtime: call
NetworkObjectprefab as the parameter.
IMPORTANT: Calling the generic Unity
Instantiate() method will just create a broken local instance!
Destruction / Despawn
To destroy a
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.
NetworkObject can be found using
NetworkRunner.TryFindObject() by passing the
NetworkId that references the instance of that
NetworkObjects 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 indicates who has the right to change and decide what is the correct value of a
Although the concept itself is identical for all topologies in Fusion, there are functional differences.
ServerMode And HostMode
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 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, 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.
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 this
NetworkObjectwill automatically be despawned when the Player with
StateAuthorityover 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, leave
AllowStateAuthorityOverrideenabled for Prefabs and Scene Objects.
ReleaseStateAuthority(): The current State Authority releases control of the object.
RequestStateAuthority(): Can be called on a client to assign its
PlayerRefas the State Authority for the
NetworkObject. The Photon Shared Server will only grant the request if:
- The Object has
- 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 of
IStateAuthorityChangedinterface when the state authority over the object changes.
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
GetInput() is called.
Input Authority and Fusion's
INetworkInput handling are primarily meant for
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
HostMode in the future, and want to avoid refactoring later.
HasInputAuthority: will return
Runner.LocalPlayer == Object.InputAuthority. Use this in scripts to test if the
NetworkRunner.LocalPlayeris the Input Authority for this
AssignInputAuthority(): Assigns input authority to the
PlayerRefpassed to the method. This can only be changed by the State Authority of the Object.
RemoveInputAuthority(): Removes input authority from the
NetworkObjectby setting it to
PlayerRef.None. This can only be changed by the State Authority of the Object.