Search Unity

[Command] not working

Discussion in 'Multiplayer' started by Happy_Jingle, Mar 10, 2016.

  1. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine.Networking;
    6. using System;
    7. public class Player : NetworkBehaviour {
    8.     public SyncListString chatHistory = new SyncListString();
    9.     string currentMessage = "";
    10.  
    11.     public void OnGUI()
    12.     {
    13.         GUILayout.BeginHorizontal (GUILayout.Width (250));
    14.         currentMessage = GUILayout.TextField (currentMessage);
    15.         if (GUILayout.Button ("Send")) {
    16.         if (!string.IsNullOrEmpty (currentMessage.Trim ())) {
    17.             Debug.Log ("command sent");
    18.             CmdChatMessage (currentMessage);
    19.             currentMessage = string.Empty;
    20.         }
    21.         }
    22.         GUILayout.EndHorizontal ();
    23.         foreach (string msg in chatHistory) {
    24.         GUILayout.Label (msg);
    25.         }
    26.         }
    27.  
    28.     [Command]
    29.     public void CmdChatMessage(string msg){
    30.     chatHistory.Add (msg);
    31.     }
    32. }
    This seems to work on the host's side, but not on the client's side, by which I mean the host can send the command, chatHistory is updated on both sides though. The client Player is not able to send the command, when I run this on the client I do get the "command sent" message.
     
  2. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Is there any information I could provide that would be useful?
     
  3. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    Note that only game objects that have Local Player Authority can send Commands to the server. Is this game object set as local player authorative?
     
  4. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103

    Yep.
    Also I don't know if it's relevant but this object is an actual Player, from my network manager
    :
     
  5. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    I am not that familiar with SyncLists, but they seem to have a Callback delegate. You could try to use

    void Start(){
    chatHistory.Callback = DoWhatever;
    }

    Where DoWhatever is a method that will ensure the changed list is drawn on screen. I am not sure this is necessary though since you draw your sync list in the OnGUI. At the least, with this method you can check if the value is actually changed on clients.

    Hope this helps!
     
  6. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Rather than doing this I can Debug.Log the length of the chatHistory list in the Player's update function. I can confirm that chatHistory is not changed when the client calls the command.
     
  7. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    Now that I think of it, this might not work because the OnGUI code is ran for both the client and the host on both instances. You need to isolate the execution so that the input field and the send button are only visible for the local player. Something like this:

    Code (CSharp):
    1.  
    2. public void OnGUI()
    3.     {
    4.         if (isLocalPlayer) {
    5.             GUILayout.BeginHorizontal(GUILayout.Width(250));
    6.             currentMessage = GUILayout.TextField(currentMessage);
    7.             if (GUILayout.Button("Send"))
    8.             {
    9.                 if (!string.IsNullOrEmpty(currentMessage.Trim()))
    10.                 {
    11.                     Debug.Log("command sent");
    12.                     CmdChatMessage(currentMessage);
    13.                     currentMessage = string.Empty;
    14.                 }
    15.             }
    16.             GUILayout.EndHorizontal();
    17.         }
    18.         foreach (string msg in chatHistory)
    19.         {
    20.             GUILayout.Label(msg);
    21.         }
    22.     }
    23.  
    However, doing this you might want to arrange stuff differently since it breaks your layout
     
    Last edited: Mar 11, 2016
  8. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    I thought that might be the case too, tried it, didn't make a difference
    Thanks for the help and patience, it turned out this was part of the problem, I've fixed it.
     
    sovium likes this.