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…) ?
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.
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.
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 ?
My understanding is that it’s an attempt at making networking easier in the HLAPI.
“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().
Oh ok !!!
I will use more of that if in OnClientStart the value is correct !
Thx a lot for the tips !