Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SyncList Callback only called when changed, but not when first transferred to the client

Discussion in 'Multiplayer' started by tinman, Sep 30, 2015.

  1. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    I have a SyncListStruct for which I have a callback, which I declare on OnStartClient.

    Everything works fine and it's syncing - but the callback is called only with the first change of the list(and following changes). When the client first receives the info, the callback is not called. Is there a way to have it called also when the client receives the list the first time?

    Thanks.
     
  2. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    Yep, the docs state the callback is only done for incremental updates. The function "OnStartClient" is called the first time the variable is setup though.

    Your SyncVars are guaranteed to be "up to date" before OnStartClient is called so you can do your modifications then. eg:

    Code (CSharp):
    1.  
    2.    [SyncVar(hook="OnSync")]
    3.    var myBool = false;
    4.  
    5.     public override void OnStartClient() {
    6.         // Called when spawned remotely by server.
    7.         Init();
    8.     }
    9.  
    10.    void OnSync(bool newVal) {
    11.       myBool = newVal;
    12.       Init();
    13.    }
    14.  
    15.    void Init() {
    16.       if (myBool) { ... }
    17.    }
    18.  
     
  3. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Ah thanks very much, good to know that it will for sure be up to date then. Question, where do you see that guarantee in the docs? I believe I saw some connection flow somewhere in the docs, but can't seem to find it right now :)
     
  4. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229