Search Unity

Question Assigning Tags to "Players" and/or "Host"

Discussion in 'Netcode for GameObjects' started by edmartine418, Jun 13, 2021.

  1. edmartine418

    edmartine418

    Joined:
    Jan 7, 2020
    Posts:
    3
    As the title describes I am having an issue assigning tags to players when using MLAPI before and after server boot. Whenever I assign a tag to the player, the tag is applied to all players in the existing lobby and to make matters worse my other computers are able to use different tags meaning that the server isn't retaining and loading the data for individuals but setting all of the players to one static tag. The problem is most likely one dealing with the code side of things and since I'm brand new to servers and multiplayer entirely it would be helpful if someone could point out any links I may be missing in my chain of commands.
    I am currently modifying one of the labs I have been learning from in order to play with something that already works and have added the following lines:​
    • HelloWorldPlayer.cs
    Code (CSharp):
    1. using MLAPI;
    2. using MLAPI.Messaging;
    3. using MLAPI.NetworkVariable;
    4. using UnityEngine;
    5.  
    6. //How do you assign a tag to each player using MLAPI?
    7. //How do you apply rules to a tag after the server starts and new players join?
    8. namespace HelloWorld
    9. {
    10.     public class HelloWorldPlayer : NetworkBehaviour
    11.     {
    12.         //This function makes the server the only one able to modify positions. (Any other way and you wouldnt have a syncronized game)
    13.         public NetworkVariableVector3 Position = new NetworkVariableVector3(new NetworkVariableSettings
    14.         {
    15.             WritePermission = NetworkVariablePermission.ServerOnly,
    16.             ReadPermission = NetworkVariablePermission.Everyone
    17.         });
    18.  
    19.         public override void NetworkStart()
    20.         {
    21.             //Changes the players color upon clicking host or client (This is a rule for all players including host and server)
    22.             if (NetworkManager.Singleton.IsClient)
    23.             {
    24.                 gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
    25.             }
    26.             else
    27.             {
    28.                 gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
    29.             }
    30.            
    31.             Move();
    32.         }
    33.  
    34.         public void Move()
    35.         {
    36.             NetworkManager.print("Hi");
    37.             if (NetworkManager.Singleton.IsServer)
    38.             {
    39.                 //if host is selected all players will be blue
    40.                 var randomPosition = GetRandomPositionOnPlane();
    41.                 transform.position = randomPosition;
    42.                 Position.Value = randomPosition;
    43.                 //gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
    44.             }
    45.             else
    46.             {
    47.                 //if client is selected all players will be red
    48.                 SubmitPositionRequestServerRpc();
    49.                 //gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
    50.             }
    51.         }
    52.  
    53.         [ServerRpc]
    54.         void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
    55.         {
    56.             Position.Value = GetRandomPositionOnPlane();
    57.         }
    58.  
    59.         static Vector3 GetRandomPositionOnPlane()
    60.         {
    61.             return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
    62.         }
    63.  
    64.         void Update()
    65.         {
    66.             transform.position = Position.Value;
    67.         }
    68.     }
    69. }
    • HelloWorldManager.cs
    Code (CSharp):
    1. using MLAPI;
    2. using UnityEngine;
    3.  
    4. namespace HelloWorld
    5. {
    6.     public class HelloWorldManager : MonoBehaviour
    7.     {
    8.        
    9.         private void Start()
    10.         {
    11.             //Important for linux server (It will scale infinitly if not set)
    12.             Application.targetFrameRate = 30;
    13.         }
    14.         void OnGUI()
    15.         {
    16.             //The graphical buttons you see on the screen are handled here.
    17.             GUILayout.BeginArea(new Rect(10, 10, 300, 300));
    18.             if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
    19.             {
    20.                 StartButtons();
    21.             }
    22.             else
    23.             {
    24.                 StatusLabels();
    25.  
    26.                 SubmitNewPosition();
    27.             }
    28.  
    29.             GUILayout.EndArea();
    30.         }
    31.  
    32.         static void StartButtons()
    33.         {
    34.             //Static start button and what they start upon being clicked.
    35.             if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
    36.             if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient();
    37.             if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
    38.         }
    39.  
    40.         static void StatusLabels()
    41.         {
    42.             //Change the labels of the buttons if "Host"/"Client"/"Server" buttons are pressed.
    43.             var mode = NetworkManager.Singleton.IsHost ?
    44.                 "Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
    45.  
    46.             GUILayout.Label("Transport: " +
    47.                 NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType().Name);
    48.             GUILayout.Label("Mode: " + mode);
    49.         }
    50.        
    51.         static void SubmitNewPosition()
    52.         {
    53.             //If the player is connected to the server, and they clicked the "Move"/"Request Position Change" button, move the player.
    54.             if (GUILayout.Button(NetworkManager.Singleton.IsServer ? "Move" : "Request Position Change"))
    55.             {
    56.                 if (NetworkManager.Singleton.ConnectedClients.TryGetValue(NetworkManager.Singleton.LocalClientId,
    57.                     out var networkedClient))
    58.                 {
    59.                    
    60.                     var player = networkedClient.PlayerObject.GetComponent<HelloWorldPlayer>();
    61.                     player.Move();
    62.                 }
    63.             }
    64.         }
    65.     }
    66. }
    The functionality I am looking for is to change the color of a cylinder's mesh renderer based on whether the prefab created was the host or a player. (This is the easiest thing I could think of but it would be useful to know when creating a team system, appending a name to the top of a player, or trying to give special privileges to the host of a server.)
     
  2. edmartine418

    edmartine418

    Joined:
    Jan 7, 2020
    Posts:
    3
    After poking around on other forums my solution may have something to do with RPC's establishing who is what in the game. The tutorial I was following though has a broken link to more information on the RPC used on the site. Does anyone have the MLAPI's RPC documentation link? Things are finally heating up again!

    The site I mentioned: https://docs-multiplayer.unity3d.com/docs/tutorials/goldenpath
     
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    I'm not sure how to easily answer your questions here. I think your questions might get be solved by understanding the basic building blocks of MLAPI better. I can recommend to look at some of the tutorials from Dapper Dino such as this one to better understand the basics:


    Where on that doc page is the broken link? We'll need to fix that.
     
  4. edmartine418

    edmartine418

    Joined:
    Jan 7, 2020
    Posts:
    3
    THAT'S EXACTLY WHAT I NEEDED!!! Thanks so much man!