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.
Adding/Removing NetworkBehaviours at Runtime
Once a Network Object has been Spawned, NetworkBehaviours cannot be added or removed.
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. 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.
- 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.
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