Search Unity

I never use SyncVar, is it bad ?

Discussion in 'Multiplayer' started by Driiades, Dec 3, 2016.

  1. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Hello,

    I never use a SyncVar, I prefere send command and RPC instead. And when I want to synchronise a variable in the beginning of game i send a message or Rpc in the ServerConnect call back.

    If i have a continuous variable who change over the time, I send message/Rpc/CMD in a ReliableStateUpdate channel ...

    Is there a significant difference between a RPC/CMD vs a SyncVar (which call a hook...) ?
     
    Last edited: Dec 3, 2016
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    SyncVars are easier than RPCs, since you can just put [SyncVar] in front of your variable, then UNET will synchronize it automatically.

    But then again you don't have to use it. RPCs work fine too. Sending raw messages works fine too.
     
  3. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    An advantage of SyncVar, is when a player joins the server late, their value is pre-set to the latest value on the host - using Rpc/Cmd, the late joining player would have to wait for that next Rpc before getting the latest version of the value. Can be helpful, depending on your messaging architecture/setup.
     
  4. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    I send a Rpc/message automatically when a player become ready. So he always have the last value.
    What is not very convenient with syncVar, is that you never know when a variable is set for the first time and you need a networkBehaviour where message doesn't care for example.

    That's why i really doesn't know when to use it. Maybe for value who doens't care to be set for the first time ?
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    My understanding is that it's an attempt at making networking easier in the HLAPI.
     
  6. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    "Set for the first time"? When a component is spawned, by the time OnStartClient is called, the syncvar will be set to whatever it was on the host/server when the host called the Spawn method. So you can check that value right away and it will be "correct". In the same way that Spawn ensures that the position data is pushed, all syncvars are also pushed, to ensure proper setup at OnClientStart().
     
  7. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    Oh ok !!!!

    I will use more of that if in OnClientStart the value is correct !
    Thx a lot for the tips !