Search Unity

Third Party Mirror - Authority issues

Discussion in 'Multiplayer' started by TheRaider, Jan 19, 2021.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I have been trying to learn how to use mirror to make a simple chat. I have been running into authority issues for the command. My script is the same as the tutorial but it is a bit a old.

    I have a networkmanger on one object and a network identity and this script on another

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using UnityEngine;
    5. using Mirror;
    6. using TMPro;
    7.  
    8. public class Chat : NetworkBehaviour
    9. {
    10.     public TMP_Text textOutPut;
    11.     public TMP_InputField inputText;
    12.     private static event Action<string> OnMessage;
    13.  
    14.     public override void OnStartAuthority()
    15.     {
    16.  
    17.         OnMessage += HandleNewMessage;
    18.     }
    19.  
    20.     [ClientCallback]
    21.     private void OnDestroy()
    22.     {
    23.         if (!hasAuthority) { return; }
    24.  
    25.         OnMessage -= HandleNewMessage;
    26.     }
    27.  
    28.     private void HandleNewMessage(string message)
    29.     {
    30.         Debug.Log("recieve message");
    31.         textOutPut.text += message;
    32.     }
    33.  
    34.     [Client]
    35.     public void Send(string message)
    36.     {
    37.  
    38.         if (string.IsNullOrWhiteSpace(message)) { return; }
    39.         if(hasAuthority)
    40.             CmdSendMessage(message);
    41.  
    42.    
    43.     }
    44.  
    45.     [Client]
    46.     public void Send()
    47.     {
    48.         Debug.Log("calling send");
    49.         if (string.IsNullOrWhiteSpace(inputText.text)) { return; }
    50.  
    51.         //if(hasAuthority)
    52.         //    CmdSendMessage(inputText.text);
    53.         CmdSendMessage(inputText.text);
    54.  
    55.         inputText.text = string.Empty;
    56.     }
    57.  
    58.     [Command]
    59.     private void CmdSendMessage(string message)
    60.     {
    61.         Debug.Log("calling cmd");
    62.         RpcHandleMessage($"[{connectionToClient.connectionId}]: {message}");
    63.     }
    64.  
    65.     [ClientRpc]
    66.     private void RpcHandleMessage(string message)
    67.     {
    68.         Debug.Log("calling rpc");
    69.         OnMessage?.Invoke($"\n{message}");
    70.     }
    71. }
    I gather I don't have the authority to call CmdSendMessage but I don't how I should be doing this properly/how to fix the error.

    I tried using if(hasauthority) but then it gives me no error but doesn't call the command.


    I think some of my issues stem from the player object this is attached to not being spawned by the network manager. Now that I have changed it to do that it kind of works and the last connected client can send to themselves but not to anyone else. If I connect another client they get that power.

    One more edit. I have figured the last connected client ends up with authority. I don't know how to give authority back to the previously connected clients I think it my problem.

    It works if i replace [command] with [Command(ignoreAuthority = true)] but I assume that isn't how you are meant to do it!
     
    Last edited: Jan 19, 2021
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    any tips on how to do this properly? I feel using [Command(ignoreAuthority = true)] is a really bad idea.

    I would love to know how to do it properly.