Search Unity

Third Party Need some help with understanding Mirror.

Discussion in 'Multiplayer' started by svinoman, Aug 21, 2022.

  1. svinoman

    svinoman

    Joined:
    Mar 18, 2019
    Posts:
    2
    Hi everyone. I am trying to understand how to work with Mirror. However, I ran into the problem of not being able to synchronize a gameobject between clients. That's why I made this simple script to figure out what went wrong.

    Code (CSharp):
    1. using UnityEngine;
    2. using Mirror;
    3.  
    4. public class MirrorTest : NetworkBehaviour
    5. {
    6.     [SerializeField] private GameObject mirrorGameObject;
    7.     private void Update()
    8.     {
    9.         if (Input.GetKey(KeyCode.Z))
    10.         {
    11.             mirrorGameObject.SetActive(true);
    12.         }
    13.         if (Input.GetKey(KeyCode.X))
    14.         {
    15.             mirrorGameObject.SetActive(false);
    16.         }
    17.     }
    18. }
    It turns out that I know nothing about Mirror at all. I can't even figure out how to synchronize this simple action.
    I know that my request might be stupid, but I just can't understand it myself.
     
  2. JamesFrowenDev

    JamesFrowenDev

    Joined:
    Oct 10, 2015
    Posts:
    20
    Most of the time for synchronize you'll want to use [SyncVar] attribute on a field

    I suggest looking at the examples that come with mirror because they will show how to do some of the basic stuff.

    There is also more information on synchronization in the mirror docs
    https://mirror-networking.gitbook.io/docs/guides/synchronization
     
    svinoman likes this.
  3. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Code (CSharp):
    1. public GameObject childObj; // local reference of object
    2. // make sure child scripts and gameobjects doesnt have network identity or use network behaviour
    3. // if it needs those, it can use its parent
    4.  
    5. [SyncVar(hook = nameof(OnChanged))]
    6. public bool objActive = true;
    7.  
    8. void OnChanged(bool _Old, bool _New)
    9. {
    10. // syncvar objActive is changed before hooks called, you can also use _New.
    11.  childObj.SetActive(objActive);
    12. }
    13.  
    14.  
    15. [Command]
    16. public void CmdChangeStatus(bool _value)
    17. {
    18. // you can add cheat checks here, and in the hook
    19.     objActive = _value;
    20. }
    21.  
    22. private void Update()
    23. {
    24. // so only your player runs the code, can use hasAuthority
    25.  if( isLocalPlayer == false ) return;
    26.  
    27.         if (Input.GetKey(KeyCode.Z))
    28.         {
    29.             CmdChangeStatus(true);
    30.         }
    31.         if (Input.GetKey(KeyCode.X))
    32.         {
    33.             CmdChangeStatus(false);
    34.         }
    35. }
    36.  
    37.  
    All this presumes this script is on your player.
    We use sync var and hook in this case, as it 'buffers' to new joining players, so late joiners get current sync var status, and that calls the hook. Takes a lot of the manual work away, they're very handy :)

    Wrote on my phone so havnt tested, good luck!
    Edit: added more info
     
    Last edited: Aug 21, 2022
    svinoman likes this.
  4. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
  5. svinoman

    svinoman

    Joined:
    Mar 18, 2019
    Posts:
    2
    Thank you so much!

    It is good to know how to do it the right way. This mess of a code is what I made myself to make it work if anyone is interested:
    Code (CSharp):
    1. using UnityEngine;
    2. using Mirror;
    3.  
    4. public class MirrorTest : NetworkBehaviour
    5. {
    6.     private GameObject mirrorGameObject;
    7.     private void Awake()
    8.     {
    9.         mirrorGameObject = GameObject.Find("MirrorTest");
    10.     }
    11.     private void Update()
    12.     {
    13.         if(isLocalPlayer)
    14.         {
    15.             if (Input.GetKey(KeyCode.Z))
    16.             {
    17.                 CmdClientToServerZ();
    18.             }
    19.             if (Input.GetKey(KeyCode.X))
    20.             {
    21.                 CmdClientToServerX();
    22.             }
    23.         }
    24.     }
    25.  
    26.     [Command]
    27.     void CmdClientToServerZ()
    28.     {
    29.         RpcServerToClientZ();
    30.     }
    31.     [ClientRpc]
    32.     void RpcServerToClientZ()
    33.     {
    34.         mirrorGameObject.SetActive(true);
    35.     }
    36.     [Command]
    37.     void CmdClientToServerX()
    38.     {
    39.         RpcServerToClientX();
    40.     }
    41.     [ClientRpc]
    42.     void RpcServerToClientX()
    43.     {
    44.         mirrorGameObject.SetActive(false);
    45.     }
    46. }
     
  6. christopherkardel

    christopherkardel

    Joined:
    Jan 1, 2022
    Posts:
    1