Search Unity

[SyncVar] <user-defined struct> not syncing to client

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

  1. sstadelman

    sstadelman

    Joined:
    Dec 17, 2013
    Posts:
    12
    I have an issue where my [SyncVar] float properties are being successfully synched from server to clients, but my [SyncVar] <user-defined struct> property is not.

    According to the manual,
    I'm fine with this--the values will only be updated once every few minutes, so efficiency is not a major concern.

    My user-defined struct follows below. The struct contains float arrays, but also according to the manual,
    Keep in mind that I am syncing only a single struct, not a list, but presumably the same rules apply to the contents of the single struct as to a member of list of structs.
    Code (CSharp):
    1. public struct MyStruct
    2. {
    3.     public float[] Values1;
    4.     public float[] Values2;
    5.     public float[] Values3;
    6.     public float[] Values4;
    7. }
    The implementation in my NetworkBehaviour can summarized as follows:
    Code (CSharp):
    1. [SyncVar(hook="MyStructChanged")] MyStruct structValue;
    2.  
    3. // The event handler is only called on a server runtime, next to a library
    4. // broadcasting value updates.
    5. // Note:  this is the same pattern used for successfully setting
    6. // [SyncVar] float values.  I suspected that I could try firing a [Command]
    7. // from here, but since the float vars are working, it seems the issue is
    8. // something different
    9.  
    10. void SomeEventHandler(object sender, BroadcastArgs e)
    11. {
    12.     structValue.Values1 = e.values1
    13. }
    14.  
    15. // hook is never invoked
    16. [Client]
    17. void MyStructChanged(MyStruct newValue)
    18. {
    19.     structValue = newValue;
    20.     UpdateUI();
    21. }
    22.  
    The point which jumps out to me in the manual but which is not clearly documented is the note: "Note that setting a SyncVar member variable inside a property setter function does not cause it to be dirtied." This seems similar to the issue described here. I suspect I need to explicitly invoke some `structValue.SetDirty()` function, but I'm not seeing an example of this approach. Am I on the right track?
     
    glitchers likes this.
  2. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    I believe the intended use is to recreate the struct each time (at least, this works for me). Pass in a new MyStruct instead of altering the variables of the existing struct.
     
    sstadelman likes this.
  3. sstadelman

    sstadelman

    Joined:
    Dec 17, 2013
    Posts:
    12
    That did it. Thanks!
     
  4. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Alternatively you can manually SetDirtyBit. It would be nice if changing the struct automatically SetDirtyBit