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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UNET SyncVars vs Messages

Discussion in 'UNet' started by RicheyMB, Aug 2, 2016.

  1. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    I've been trying to get my head around Unet and was hoping someone could explain what kind of cost (if any) SyncVars have in terms of network traffic.

    I notice that NetworkTransform does not use SyncVars or Commands to communicate between server and client - is this because it cuts down on traffic or because the code is managing many scenarios?

    Thanks in advance.
     
  2. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I'm not really an authority in the field but I was just looking at NetworkIdentity's UNetUpdate() method in unity networking's source code and it seems that SyncVars are really just things that detect if a value has changed and then sends an actual Message (of the MsgType.UpdateVars type) to all valid connections

    So basically, seems to me like SyncVars and Messages probably have the same cost
     
  3. RicheyMB

    RicheyMB

    Joined:
    Jul 20, 2009
    Posts:
    111
    Thanks for the reply. I didn't realise they got sent via the network identity. I shall investigate that code.
     
  4. isidro02139

    isidro02139

    Joined:
    Jul 31, 2012
    Posts:
    72
    One comment/question: if you set a SynVar's value to the same thing (i.e. set a [SyncVar] bool that is 'false' to 'false') will this trigger any hook functions on the SyncVar?

    I didn't expect that would be the case (i.e. to cut down on traffic) but in an earlier test it seems to trigger the hook function... Just wondering if anyone knows definitively. Next time I encounter that situation and discover the truth I will share here.

    Arun
     
  5. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,103
    Whenever unity updates the syncVars using it's update message it sends an additional int which is used as a bit field which says which of the vars are updated. So depends on your scenario.
    If you will send a message per variable update and you only have one update, messages are better. If you have multiple variables, then syncvars send them all together and that one int probably is smaller than the message header of the multiple messages combined. You can create your own system which is not general/generic and sends multiple variables together but without the bitfield and be more optimized however but you can not use it as a general solution for everything.