Skip to main content

serialization-flow

Serialization Flow

Game objects with the Network Identity component attached can have multiple scripts derived from NetworkBehaviour. The flow for serializing these game objects is:

On the server:

  • 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 game objects are checked on the server as part of its update loop
  • If any NetworkBehaviours on a NetworkIdentity are dirty, then a UpdateVars packet is created for that game object
  • The UpdateVars packet is populated by calling OnSerialize on each NetworkBehaviour on the game 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 does not send again until its value changes.
  • The UpdateVars packet is sent to ready clients that are observing the game object

On the client:

  • an UpdateVars packet is received for a game object
  • The OnDeserialize function is called for each NetworkBehaviour script on the game object
  • Each NetworkBehaviour script on the game object reads a dirty mask.
  • If the dirty mask for a NetworkBehaviour is zero, the OnDeserialize function returns without reading any more
  • If the dirty mask is a 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.