Network Object
Component Overview
A Network Object is a GameObject
with a NetworkObject
component, and represents a single network entity in a Room
Network Objects can be created either by Spawning or by loading as Scene Objects.
NetworkId
The Server assigns a NetworkId
value to the NetworkObject
component, which is an unique integer identifier for that Object in the Room. This NetworkId
is consistent among all peers and is used to reference the Object on the network.
The local Network Object instance can be found with this NetworkId
by calling NetworkRunner.TryFindObject()
.
Spawning Network Objects
Network Objects are produced created in one of two ways;
- Calling
Runner.Spawn()
When Spawn is called, a GameObject with a NetworkObject component is produced, and the Spawn action is replicated on the Network. The INetworkObjectProvider
that was passed inRunner.StartGame()
contains the implementation for how the spawned object is produced - be it Instantiation of a Prefab, cloning an existing object, creating a new custom Object with code, or pulling the Object from a pool. - Loading a scene containing a Network Object(s)
See Scene Management
Both methods result in the object being produced, and replicating that Network Object and its State to all interested Peers.
Creating a NetworkObject with Runner.Spawn()
Runner.Spawn() can only be called on the Server in Host Mode, or on the client that intends to be the State Authority of the spawned Object in Shared Mode. When called, the specified Prefab is instantiated, and then its NetworkObject
component is attached to the NetworkRunner
. The Spawn will be replicated to other clients which have Interest in the Object.
Prefab / Object Table
Spawning Network Objects typically involves passing a Prefab instance, which is used for Instantiation. 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.
Adding/Removing NetworkBehaviours at Runtime
Once a Network Object has been Spawned, NetworkBehaviours cannot be added or removed. However, it is possible to add/remove NetworkBehaviours at runtime PRIOR to spawning. There are some considerations when doing this though:
- The
NetworkObject
must be baked(needs instructions here) - Instantiation for Custom Network Objects typically is handled with a custom
INetworkObjectProvider
implementation.
Link to INetworkObjectProvider
Loading Scene Network Objects
Loading Network Objects with scenes are another method of producing and attaching Objects. When a scene is loaded using the ISceneManager
instance (see Scene Management) any enabled Network Objects in the scene are attached on the Master Client or Server in Server Mode. Then the assigned NetworkId
of each Scene Object is communicated to all other clients, and those clients attach the loaded Scene Objects and assign them those NetworkId
values.
Attaching
When an Network Object is spawned or loaded with a scene, it is Attached. Attaching is the process of assigning a Network Object its NetworkId
, allocating memory for Networked Properties of it NetworkBehaviour
components, and initializing all associated NetworkBehaviours
. Once Attached, the NetworkObject
will call Spawned()
on all child Network Behaviours. Once all those Spawned()
methods have been called, the Network Object is considered valid and its Networked Properties and RPC methods will be valid and accessible. All of the associated event methods like FixedUpdateNetwork()
and Render()
and interfaces will begin getting called.
State Authority
Every attached Network Object has an explicit or implied State Authority, which is designated by a PlayerRef. If the Runner.StateAuthority
value is PlayerRef.None
, then the Server Peer is the implied State Authority.
Server Modes (Host/Dedicated/Single)
Runner.StateAuthority
will always be PlayerRef.None
- as the Server is ALWAYS the State Authority. State Authority cannot be changed.
Shared Server Mode
NetworkObject.StateAuthority
will always be a valid PlayerRef
. State Authority can change, with some restrictions:
- State Authority CANNOT be assigned. A Player (even if they currently have State Authority) cannot assign State Authority to another Player.
- State Authority CAN be acquired. A Player can call
Object.RequestStateAuthority()
to try and acquire authority.
RequestStateAuthority()
will only succeed if;
NetworkObject.AllowStateAuthorityOverride
was set to true, OR;- The previous State Authority has called
Object.ReleaseStateAuthority()
The IStateAuthorityChanged
interface can be used to detect changes of StateAuthority on an object. OnStateAuthorityChanged
is called on every client whenever the StateAuthority
of an object changes.
Input Authority
Input Authority is specific to Server Modes (Host, Dedicated, Single) and is not applicable in Shared Server Mode. The Input Authority indicates which Player’s inputs should be returned for this Object when GetInput()
is called.
Replication
Once attached, a Network Object simulates with each Tick, and the State Authority replicates the resulting State to all other Peers.
Important: Network Properties only replicate from the State Authority to other peers. Any changes made to Networked Properties on other peers will be overwritten when the next authority state arrives. In the case of the Input Authority, this is a normal part of prediction, as it will simulate ahead of the received Server State - and rollback every time a new server state arrives and resimulate forward again.
Interest Management
Which players a Network Object replicates to can be culled using various Interest Management mechanisms, such as Area Of Interest. Interest Management is a data reduction mechanism based on excluding updates of Objects to certain players, and is very important for games with a large number of NetworkObjects and/or high Player counts.
See Interest Management
Nesting
Fusion supports nesting Network Objects. Nesting refers to one Network Object GameObject being a child of another in the Hierarchy. Scene Objects and Spawned Prefabs can be multiple Network Objects on one GameObject Prefab. When attached, each Network Object is treated as a separate entity from its Parent and Children Network Objects. Network Behaviours will be associated with the most direct Parent, and root Network Objects will not attach Network Behaviours that belong to a nested child Network Object.
Back to top