Mirror::NetworkIdentity
The NetworkIdentity identifies objects across the network, between server and clients. Its primary data is a NetworkInstanceId which is allocated by the server and then set on clients. This is used in network communications to be able to lookup game objects on different machines. More...
Inherits from MonoBehaviour
Public Events
Name | |
---|---|
ClientAuthorityCallback | clientAuthorityCallback() A callback that can be populated to be notified when the client-authority state of objects changes. |
Public Functions
Name | |
---|---|
NetworkIdentity | GetSceneIdentity(ulong id) Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id |
void | ResetNextNetworkId() Resets nextNetworkId = 1 |
delegate void | ClientAuthorityCallback(NetworkConnection conn, NetworkIdentity identity, bool authorityState) The delegate type for the clientAuthorityCallback. |
void | RebuildObservers(bool initialize) This causes the set of players that can see this object to be rebuild. The OnRebuildObservers callback function will be invoked on each NetworkBehaviour. |
bool | AssignClientAuthority(NetworkConnection conn) Assign control of an object to a client via the client's NetworkConnection. |
void | RemoveClientAuthority() Removes ownership for an object. |
Public Properties
Name | |
---|---|
bool | isClient Returns true if running as a client and this object was spawned by a server. |
bool | isServer Returns true if NetworkServer.active and server is not stopped. |
bool | hasAuthority This returns true if this object is the authoritative player object on the client. |
uint | netId Unique identifier for this particular object instance, used for tracking objects between networked clients and the server. |
NetworkConnection | connectionToServer The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on a local client. |
NetworkConnectionToClient | connectionToClient The NetworkConnection associated with this NetworkIdentity. This is valid for player and other owned objects in the server. |
NetworkBehaviour [] | NetworkBehaviours |
NetworkVisibility | visibility |
Guid | assetId Unique identifier used to find the source assets when server spawns the on clients. |
bool | SpawnedFromInstantiate |
Public Attributes
Name | |
---|---|
bool | isLocalPlayer This returns true if this object is the one that represents the player on the local machine. |
Dictionary< int, NetworkConnection > | observers The set of network connections (players) that can see this object. |
ulong | sceneId A unique identifier for NetworkIdentity objects within a scene. |
bool | serverOnly Flag to make this object only exist when the game is running as a server (or host). |
readonly Dictionary< uint, NetworkIdentity > | spawned All spawned NetworkIdentities by netId. Available on server and client. |
Detailed Description
class Mirror::NetworkIdentity;
The NetworkIdentity identifies objects across the network, between server and clients. Its primary data is a NetworkInstanceId which is allocated by the server and then set on clients. This is used in network communications to be able to lookup game objects on different machines.
The NetworkIdentity is used to synchronize information in the object with the network. Only the server should create instances of objects which have NetworkIdentity as otherwise they will not be properly connected to the system.
For complex objects with a hierarchy of subcomponents, the NetworkIdentity must be on the root of the hierarchy. It is not supported to have multiple NetworkIdentity components on subcomponents of a hierarchy.
NetworkBehaviour scripts require a NetworkIdentity on the game object to be able to function.
The NetworkIdentity manages the dirty state of the NetworkBehaviours of the object. When it discovers that NetworkBehaviours are dirty, it causes an update packet to be created and sent to clients.
The flow for serialization updates managed by the NetworkIdentity is:
Each NetworkBehaviour has a dirty mask. This mask is available inside OnSerialize as syncVarDirtyBits Each SyncVar in a NetworkBehaviour script is assigned a bit in the dirty mask. Changing the value of SyncVars causes the bit for that SyncVar to be set in the dirty mask Alternatively, calling SetDirtyBit() writes directly to the dirty mask NetworkIdentity objects are checked on the server as part of it's update loop If any NetworkBehaviours on a NetworkIdentity are dirty, then an UpdateVars packet is created for that object The UpdateVars packet is populated by calling OnSerialize on each NetworkBehaviour on the object NetworkBehaviours that are NOT dirty write a zero to the packet for their dirty bits NetworkBehaviours that are dirty write their dirty mask, then the values for the SyncVars that have changed If OnSerialize returns true for a NetworkBehaviour, the dirty mask is reset for that NetworkBehaviour, so it will not send again until its value changes. The UpdateVars packet is sent to ready clients that are observing the object
On the client: an UpdateVars packet is received for an object The OnDeserialize function is called for each NetworkBehaviour script on the object Each NetworkBehaviour script on the object reads a dirty mask. If the dirty mask for a NetworkBehaviour is zero, the OnDeserialize functions returns without reading any more If the dirty mask is non-zero value, then the OnDeserialize function reads the values for the SyncVars that correspond to the dirty bits that are set If there are SyncVar hook functions, those are invoked with the value read from the stream.
Public Events Documentation
event clientAuthorityCallback
static ClientAuthorityCallback clientAuthorityCallback()
A callback that can be populated to be notified when the client-authority state of objects changes.
Whenever an object is spawned with client authority, or the client authority status of an object is changed with AssignClientAuthority or RemoveClientAuthority, then this callback will be invoked.
This callback is only invoked on the server.
Public Functions Documentation
function GetSceneIdentity
static NetworkIdentity GetSceneIdentity(
ulong id
)
Gets the NetworkIdentity from the sceneIds dictionary with the corresponding id
Parameters:
- id
Return: NetworkIdentity from the sceneIds dictionary
function ResetNextNetworkId
static void ResetNextNetworkId()
Resets nextNetworkId = 1
function ClientAuthorityCallback
delegate void ClientAuthorityCallback(
NetworkConnection conn,
NetworkIdentity identity,
bool authorityState
)
The delegate type for the clientAuthorityCallback.
Parameters:
- conn The network connection that is gaining or losing authority.
- identity The object whose client authority status is being changed.
- authorityState The new state of client authority of the object for the connection.
function RebuildObservers
inline void RebuildObservers(
bool initialize
)
This causes the set of players that can see this object to be rebuild. The OnRebuildObservers callback function will be invoked on each NetworkBehaviour.
Parameters:
- initialize True if this is the first time.
function AssignClientAuthority
inline bool AssignClientAuthority(
NetworkConnection conn
)
Assign control of an object to a client via the client's NetworkConnection.
Parameters:
- conn The connection of the client to assign authority to.
Return: True if authority was assigned.
This causes hasAuthority to be set on the client that owns the object, and NetworkBehaviour.OnStartAuthority will be called on that client. This object then will be in the NetworkConnection.clientOwnedObjects list for the connection.
Authority can be removed with RemoveClientAuthority. Only one client can own an object at any time. This does not need to be called for player objects, as their authority is setup automatically.
function RemoveClientAuthority
inline void RemoveClientAuthority()
Removes ownership for an object.
This applies to objects that had authority set by AssignClientAuthority, or [NetworkServer.Spawn] with a NetworkConnection parameter included.
Authority cannot be removed for player objects.
Public Property Documentation
property isClient
bool isClient;
Returns true if running as a client and this object was spawned by a server.
IMPORTANT: checking NetworkClient.active means that isClient is false in OnDestroy:
public bool isClient => NetworkClient.active && netId != 0 && !serverOnly;
but we need it in OnDestroy, e.g. when saving skillbars on quit. this works fine if we keep the UNET way of setting isClient manually.
=> fixes
property isServer
bool isServer;
Returns true if NetworkServer.active and server is not stopped.
IMPORTANT: checking NetworkServer.active means that isServer is false in OnDestroy:
public bool isServer => NetworkServer.active && netId != 0;
but we need it in OnDestroy, e.g. when saving players on quit. this works fine if we keep the UNET way of setting isServer manually.
=> fixes
property hasAuthority
bool hasAuthority;
This returns true if this object is the authoritative player object on the client.
This value is determined at runtime. For most objects, authority is held by the server.
For objects that had their authority set by AssignClientAuthority on the server, this will be true on the client that owns the object. NOT on other clients.
property netId
uint netId;
Unique identifier for this particular object instance, used for tracking objects between networked clients and the server.
This is a unique identifier for this particular GameObject instance. Use it to track GameObjects between networked clients and the server.
property connectionToServer
NetworkConnection connectionToServer;
The NetworkConnection associated with this NetworkIdentity. This is only valid for player objects on a local client.
property connectionToClient
NetworkConnectionToClient connectionToClient;
The NetworkConnection associated with this NetworkIdentity. This is valid for player and other owned objects in the server.
Use it to return details such as the connection's identity, IP address and ready status.
property NetworkBehaviours
NetworkBehaviour [] NetworkBehaviours;
property visibility
NetworkVisibility visibility;
property assetId
Guid assetId;
Unique identifier used to find the source assets when server spawns the on clients.
The AssetId trick: Ideally we would have a serialized 'Guid m_AssetId' but Unity can't serialize it because Guid's internal bytes are private UNET used 'NetworkHash128' originally, with byte0, ..., byte16 which works, but it just unnecessary extra code Using just the Guid string would work, but it's 32 chars long and would then be sent over the network as 64 instead of 16 bytes
The solution is to serialize the string internally here and then use the real 'Guid' type for everything else via .assetId
property SpawnedFromInstantiate
bool SpawnedFromInstantiate;
Public Attributes Documentation
variable isLocalPlayer
bool isLocalPlayer => ClientScene.localPlayer == this;
This returns true if this object is the one that represents the player on the local machine.
This is set when the server has spawned an object for this particular client.
variable observers
Dictionary< int, NetworkConnection > observers;
The set of network connections (players) that can see this object.
null until OnStartServer was called. this is necessary for SendTo* to work properly in server-only mode.
variable sceneId
ulong sceneId;
A unique identifier for NetworkIdentity objects within a scene.
This is used for spawning scene objects on clients.
variable serverOnly
bool serverOnly;
Flag to make this object only exist when the game is running as a server (or host).
variable spawned
static readonly Dictionary< uint, NetworkIdentity > spawned = new Dictionary<uint, [NetworkIdentity](/Documentation/Cops%20And%20Robbers/Classes/classMirror_1_1NetworkIdentity/)>();
All spawned NetworkIdentities by netId. Available on server and client.
Updated on 27 January 2021 at 18:44:11 UTC