Search Unity

How to display player ID on client sides?

Discussion in 'Multiplayer' started by JooZ, Apr 11, 2014.

  1. JooZ

    JooZ

    Joined:
    May 18, 2013
    Posts:
    5
    Hi,

    I followed this great tutorial to build a multiplayer application with a specific server and each client being a player:
    http://3dgep.com/?p=4609

    In a fully authoritative mode, I don't know how to send specific variables (I mean other than the position/rotation of the transform) to each player. For example, to display the player ID on each player (cube), I've been doing the following in a ServerPlayerManager (server-side):

    Code (csharp):
    1.  
    2.     Hashtable players = new Hashtable();
    3.  
    4.     public void spawnPlayer(NetworkPlayer player) {
    5.         Debug.Log("Spawning player game object for player " + player);
    6.         PlayerInfo ply = GameObject.FindObjectOfType(typeof(PlayerInfo)) as PlayerInfo;
    7.         GameObject go = (GameObject)Network.Instantiate(ply.playerPrefab, Vector3.up*3, Quaternion.identity, 0);
    8.         players[player] = go;
    9.  
    10.         networkView.RPC("setID", RPCMode.All, player);
    11.     }
    12.  
    13.  
    14.     [RPC]
    15.     void setID(NetworkPlayer player) {
    16.         GameObject go = (GameObject)players[player];
    17.         cubeScript cSript = (cubeScript)go.GetComponent<cubeScript>();
    18.         cSript.ID = player.ToString();
    19.     }
    20.  
    In my cube/player prefab, I attached a script (observed by a NetworkView) with a public variable ID, which I link to a TextMesh.

    Problem:
    On the server side, each cube has its own label displayed correctly.
    BUT on the client side - it does not work.

    It looks like the reason is that the Hashtable "players" is empty on the client side - so the code cannot reach specifically each player displayed to update the correct ID.

    Can somebody help on that?

    Thanks!
     
  2. JooZ

    JooZ

    Joined:
    May 18, 2013
    Posts:
    5
    OK. Not sure if anyone is interested, but I found my answer and post it here in case somebody arrived later with the same question.

    The trick here is be able to get access to the networkView of the prefab itself and to call a function from there towards every other player or server.

    What I had above as setID function needs to be moved in the script attached to the player prefab:
    Code (csharp):
    1.  
    2.     [RPC]
    3.     void setID(NetworkPlayer player)
    4.     {
    5.         this.ID = player.ToString();
    6.     }
    7.  
    Then in my SpawnPlayer function above, where I instantiate the players from the server, I need to do an RPC from the prefab of the script itself. Something like:
    Code (csharp):
    1.  
    2.     Hashtable players = new Hashtable();
    3.  
    4.     public void spawnPlayer(NetworkPlayer player) {
    5.  
    6.         Debug.Log("Spawning player game object for player " + player);
    7.         PlayerInfo ply = GameObject.FindObjectOfType(typeof(PlayerInfo)) as PlayerInfo;
    8.         GameObject go = (GameObject)Network.Instantiate(ply.playerPrefab, Vector3.up*3, Quaternion.identity, 0);
    9.         players[player] = go;
    10.  
    11.         NetworkView remoteNetworkView = go.networkView;
    12.         remoteNetworkView.RPC("SetID", RPCMode.AllBuffered, player);
    13.     }
    14.  
    So the difference from what I was doing previously is the networkview on which I was running an RPC.