Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unique player ID across network

Discussion in 'Multiplayer' started by dacloo, Nov 24, 2007.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Okay.... I am totally utterly confused :)

    In the included screenshot you can see both the client and server running. What I am trying to accomplish is something simple: I want all the player names (shown as 3D text) to be the same across the network, based on an unique ID that Unity assigns to a player on the network.
    As you can see, that's not the case right now.

    Why? Well, when I receive an RPC or something, I would like to be able to say:
    1. I received a message from player ID 6
    2. So now I simply do a transform.player("player"+ID) and do something with it.

    So...I want something very easy, but I am confused: what is the unique player ID, and how do I assign it?
    I tried using msg.sender, and owner...

    Thanks!
     

    Attached Files:

  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    274
    The player ID you see in NetworkPlayer is used internally by Unity, but it is a unique identifier which can be used as that. You can't really use it as a player number in the way you want, you can never change/assign it. You need your own implementation of some player number system. Some thing like OnPlayerConnected() the server sends an RPC to the connecting player to tell him his number. This the server registers in a data structure and informs all other clients as well. Of course you can add all sorts of other information to this routine, like real player name, faction affiliation or whatever.

    -1 usually means an unitialized ID or the local ID. The client always sees the server with player ID 0. Don't know why the same cube is assigned 0 (server ID) on the client and 1 (1st client ID) on the server, suspect the code is assigning incorrectly or something.
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Larus,

    Thanks. I was suspecting that -1 ment "local". But I wasn't sure.

    So, basically, based on your post, this implementation would work?

    Code (csharp):
    1.  
    2. function OnNetworkInstantiate (msg : NetworkMessageInfo) {
    3.     if (networkView.isMine) {
    4.         transform.name="player_"+networkView.owner;
    5.             }
    6.     else {
    7.         transform.name="player_"+msg.sender;
    8.         }
    9.     }
    10.  
    Because now, I get the same player ID's on both the client and server
     
  4. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    274
    Isn't that whats supposed to happen? The newly instantiated object gets the same name on client and server, based on his player ID number. It therefore would have the same name on all clients in the game. Except that msg.sender will be the server as seen from other client. The server does the relaying. Its best not to use the unique ID number for this purpose, as I said in the post above.
     
  5. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Larus,

    Yes it is what is supposed to happen, but I was wondering if this is the way to basically do it :) (Being fool-proof).

    I'm not sure I understand. If this number is unique, why will it be the server as seen from other clients? Isn't that where other commands as "isServer" is for? To determin that?

    Hmm okay I think I'll go that route then :)
     
  6. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    274
    Well, the sender property always indicates from where the message came from. So if a client sends a network update, the server receives it and relays to all other clients. Therefore the server will see the message sender as the client, and the _other_ clients will see the message sender as the server. So you really can't rely on he sender property for the purpose of distributing a player ID number for an object. Hope that clears things up, and why one needs to implement a custom player number mechanism thingy, to get things correct.