Search Unity

Unity Multiplayer [Suggestion] Allow void type for SyncVar hooks

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

  1. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    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

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    275
    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):
    1.  
    2. //x will be synced across the network via the hook function
    3. [SyncVar(hook = "func1")]
    4. float x;
    5.  
    6. void func1(float newX) {
    7.     x = newX;
    8. }
    9.  
    10. //y will be synced automatically
    11. [SyncVar]
    12. float y;
    13.  
    14. //z will NOT be synced
    15. [SyncVar(hook = "func2")]
    16. float z;
    17.  
    18. void func2(float newZ) {
    19.  
    20. }
     
    Zenov likes this.
unityunity