Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Batched updates

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

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Hi,

    With the HLAPI, without customizing any behaviour, all [SyncVars] are being synced and updated between all connected game clients (from Server -> Client).

    1. I'd like to understand when this sync takes place? are updates to syncvars batched ? (e.g: if i have 5 different objects whose SyncVars were updated, are these updates sent at the same time?)
    2. Also, is there any control over this process ? for example, can i use SyncVars, but call a method myself that will decide when to call the update that will send the changes to the server ?
     
  2. Indiefreaks

    Indiefreaks

    Joined:
    Nov 12, 2012
    Posts:
    89
    I can't reply to point 1.
    However regarding point 2., here would be my best guess to how you may want to implement it.

    SyncVars are updated from Server => Client as you mentioned.
    Thus, you may have a Command method in the same NetworkBehaviour inherited class that would get called from within your client instance (using a [Client] attribute on an Update method for instance where you could tell whenever you want it to be changed.
    Client.Update() => Calls Command on Server => Server Command implementation changes SyncVar value => Server.SyncVar value is networked to the Client.SyncVar instance.

    I for instance used this mechanism to define for each of my players their Name.
    I have a PlayerNetworkBehaviour class which has a Nickname SyncVar string and a CmdSetNickname(string name) method.
    When the player sets his nickname using an input field I placed on an in game form, I simply call the CmdSetNickname method which will set the new nickname on the server and will be reflected locally.

    Hope it helps.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    This simply explains how to update data using commands. But what if i have 5 different NetworkBehaviours each with his own SyncVars. Can i control how these are synced ? (E.g: sync them all together)
     
  4. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    473
    I've found that the quickest way to get the most accurate information about details like serialization timing is to point an assembly browser like ILSpy at C:\Program Files\Unity\Editor\Data\UnityExtensions\Unity\Networking\UnityEngine.Networking.dll and browse the decompiled code.
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I have already looked at the internal code a few times. By the looks of it (although that code is not found in the UnityEngine.Networking.dll) is that SyncVars are promoted to properties (using the UNetWeaver), and when updated, they post the new data to be updated on the server.

    I didn't follow long enough to see WHEN the data is actually sent.
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    When the server detects an object with a NetworkIdentity is dirty it sends an update packet to clients.

    The generated syncvar serialization code in the script assembly DLL in the Library folder if you want to see it.