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

[Suggestion] Allow void type for SyncVar hooks

Discussion in 'Multiplayer' started by powdered_swords, Aug 5, 2015.

  1. powdered_swords


    Mar 11, 2013
    The current implementation of SyncVar event generation forces the user to pass a parameter to the referred function. However there are many situations when a more generalized event might be useful. For example an AppearanceChanged function which rebuilds a mesh based on multiple parameters.

    I was unable to find an easy workaround for this issue. It seems the network compiler finds the first instance of a function and will ignore all others, meaning overloaded methods like the example below wont work.

    [SyncVar(hook = "SyncVarEvent")]
    public short paramA;

    //this param throws an error
    [SyncVar(hook = "SyncVarEvent")]
    public Vector3 paramB;

    public void SyncVarEvent(short param) { SyncVarEvent(); }
    public void SyncVarEvent(Vector3 param) { SyncVarEvent(); }
    public void SyncVarEvent()
    Last edited: Aug 5, 2015
  2. Piflik


    Sep 11, 2011
    The value passed to the Hook is the value the SyncVariable is changed to. If you don't need that value, don't use it, but when you specify a Hook function for a SyncVariable, this variable will not be synced automatically. You have to change the value inside the hook for the variable to be synced across the network.

    Code (CSharp):
    2. //x will be synced across the network via the hook function
    3. [SyncVar(hook = "func1")]
    4. float x;
    6. void func1(float newX) {
    7.     x = newX;
    8. }
    10. //y will be synced automatically
    11. [SyncVar]
    12. float y;
    14. //z will NOT be synced
    15. [SyncVar(hook = "func2")]
    16. float z;
    18. void func2(float newZ) {
    20. }
    Zenov likes this.