Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnSerialize and dirty state (confused by docs)

Discussion in 'Multiplayer' started by liortal, Jun 13, 2015.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,560
    Hey,

    I am a little confused by the docs on this matter (taken from: http://docs.unity3d.com/Manual/UNetStateSync.html)
    This is followed by the following (a few lines below):
    This seems to be contradictive. Can someone please explain how the flow really goes ?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Yes, we need better examples and docs for this.

    • each networkbehaviour has a dirty mask.
    • changing SyncVars or calling SetDirtyBit() write to this 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
    • this packet is populated by calling OnSerialize on each NetworkBehaviour on the object
    • NetworkBehaviour that are NOT dirty write a zero byte to the packet for their dirty bits
    • if OnSerialize returns true, the dirty mask is reset for that NetworkBehaviour
    • the packet is sent to ready clients that are observing the object

    • on the client, an UpdateVars packet is received for an object
    • OnDeserialize is called for each NetworkBehaviour script on the object
    • scripts read a dirty mask - if it is zero they return immediately
    • non-dirty scripts read data from the stream based on the dirty bits

    This is a little more complex in practice because different scripts can use different QoS channels. In that case, there is a packet for each channel.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,560
    This is still a bit confusing - you're saying that "if any networkbehaviours on a NetworkIdentity are dirty, then an UpdateVars packet is created for that object", but then "NetworkBehaviour that are NOT dirty write a zero byte to the packet for their dirty bits".

    How is that possible?

    The only scenario i can think is that if an object has multiple NetworkBehaviour scripts, where one might be marked as dirty (e.g: i updated a SyncVar on that script) and did not update the other scripts. This will trigger an update packet, where only the NetworkBehaviour whose SyncVar i updated will need to serialize its data, and all the others won't do anything (write a zero byte as you said).
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    that is exactly correct.