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

Networking in subclass

Discussion in 'Multiplayer' started by Softelectro, Oct 31, 2016.

  1. Softelectro

    Softelectro

    Joined:
    Oct 9, 2013
    Posts:
    24
    Hello everybody,

    I need to synchronize some values between clients. I was able to synchronize value in parent class. See below.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class SynchroValue : NetworkBehaviour
    6. {
    7.     public GameObject LeftHand_Origin = null;
    8.     public GameObject LeftHand_Target = null;
    9.     [SyncVar]
    10.     public bool LeftHand_Enabled = false;
    11.     [SyncVar]
    12.     public bool LeftHand_Open = false;
    13.  
    14.     public GameObject RightHand_Origin = null;
    15.     public GameObject RightHand_Target = null;
    16.     [SyncVar]
    17.     public bool RightHand_Enabled = false;
    18.     [SyncVar]
    19.     public bool RightHand_Open = false;
    20.  
    21.  
    22.     [Command]
    23.     public void CmdLeftHand_Activate(bool status)
    24.     {
    25.         LeftHand_Enabled = status;
    26.     }
    27.  
    28.     [Command]
    29.     public void CmdLeftHand_Open(bool status)
    30.     {
    31.         LeftHand_Open = status;
    32.     }
    33.  
    34.     [Command]
    35.     public void CmdRightHand_Activate(bool status)
    36.     {
    37.         RightHand_Enabled = status;
    38.     }
    39.  
    40.     [Command]
    41.     public void CmdRightHand_Open(bool status)
    42.     {
    43.         RightHand_Open = status;
    44.     }
    45.  
    46.    void start()
    47.    {
    48.      CmdLeftHand_Activate(true);
    49.      CmdRightHand_Activate(true);
    50.    }
    51. }
    I want to simplify the code so i want to add a subclass. In this case i'm not able to synchronize values. See below new code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. [System.Serializable]
    6. public class Hand
    7. {
    8.     public GameObject Origin = null;
    9.     public GameObject Target = null;
    10.     [SyncVar]
    11.     public bool Enabled = false;
    12.     [SyncVar]
    13.     public bool Open = false;
    14.  
    15.     [Command]
    16.     public void CmdHand_Activate(bool status)
    17.     {
    18.         Enabled = status;
    19.     }
    20.  
    21.     [Command]
    22.     public void CmdHand_Open(bool status)
    23.     {
    24.         Open = status;
    25.     }
    26. }
    27.  
    28. public class SynchroValue2 : NetworkBehaviour
    29. {
    30.     public Hand LeftHand = null;
    31.     public Hand RightHand = null;
    32.  
    33.     void start()
    34.     {
    35.         LeftHand.CmdHand_Activate(true);
    36.         RightHand.CmdHand_Activate(true);
    37.     }
    38.  
    39. }
    40.  
    Is someone have got a idea/solution for my issue?
    Thanks in advance
     
  2. invulse

    invulse

    Joined:
    Nov 29, 2010
    Posts:
    128
    This is a complex problem that does not have a simple answer.

    You in essence want sync a "sub-object" over the network. As far as I can tell UNet does not support sub-object syncing, and implementing sub-object syncing is not trivial. If you just want to mimic SyncVar variables on sub-objects you could probably override OnSerialize() OnDeserialize() for your NetworkBehaviour and manually sync the properties of your sub-object, however when you override those functions you lose the ability to use SyncVar attributes.

    As for getting Commands to work in a subobject, I suspect this is not possible at all, and I would suggest moving the commands out to the behaviour that contains the sub-object.
     
  3. Softelectro

    Softelectro

    Joined:
    Oct 9, 2013
    Posts:
    24
    Ok thanks for your answer.
    I will keep the first solution.