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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get -this- networkConnectionID or -this- networkIdentityID on the server

Discussion in 'Multiplayer' started by tinman, Oct 1, 2015.

  1. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Is there a way to get the connection ID of this player object's network connection on the server? Or this objects netID (without GetComponent<NetworkIdentity>().netID (also on the server)?

    Thanks
     
  2. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    The "netId" is the unique identifier for each tracked object, and is found in the NetworkIdentity component.

    You can also get the "connectionToClient" value from NetworkIdentity; I think the server also has a list of clients (NetworkServer.connections, I think?) but picking out which one to send any commands to is difficult if you don't have a "netId" to address.

    Note that nothing will have a netId without a NetworkIdentity component, so I'm not sure what you're going for there? Maybe describe what you're trying to make?

    Here's some code I wrote that should AssignNetworkAuthority, fetches the Id numbers and looks it up on the server side (in the cmd function):

    Code (CSharp):
    1.     public void AssignNetworkAuthority() {
    2.         NetworkIdentity currentPlayer = Globals.playerTransform.GetComponent<NetworkIdentity>();
    3.         if (currentPlayer == null) {
    4.             //Debug.Log ("No player spawned yet, or playing locally.");
    5.         } else {
    6.             CmdAssignNetworkAuthority(currentPlayer.netId);
    7.         }
    8.     }
    9.  
    10.     [Command] protected void CmdAssignNetworkAuthority(NetworkInstanceId toId) {
    11.         GameObject client = NetworkServer.FindLocalObject(toId);
    12.         var conn = client.GetComponent<NetworkIdentity>().connectionToClient;
    13.         NetworkIdentity ni = GetComponent<NetworkIdentity>();
    14.         if (ni.clientAuthorityOwner != null) {
    15.             // Remove previous owner
    16.             ni.RemoveClientAuthority(ni.clientAuthorityOwner);
    17.         }
    18.         ni.AssignClientAuthority(conn);
    19.     }
    (note that the above code doesn't currently work and is a filed bug with unity, it may be internally fixed.. crossing fingers for next patch)
     
  3. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    I'm looking for a quick way to know who this client is, without the need to lookup GetComponent<NetworkIdentity>(). In the legacy system this was defined as Network.player. I was wondering if there is something similar to that in UNET.
    As a follow up:
    Is NetworkIdentity.connectionToClient.connectionID == NetworkIdentity.connectionToServer.connectionID as long as the are called on the client/server respecitvely?
     
  4. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    I could be wrong here but I don't think there's a "client ID" that we can reference anywhere, I went looking earlier. In the new system, there's instead the concept of ID numbers for specific game objects. Imagine a game where you have two players on each networked computer (co-op online?), each individual player will have their own ID as opposed to the "computer/client" itself.

    In a test I just ran, connectionID is NOT equal on client/server, that seems to be locally set? (which seems silly to me).
     
  5. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Wow, that is somewhat silly!
    So when a player connects to the server , I guess the ID given is connection on the server is
    connectionToClient.connectionID?

    For example:

    Code (CSharp):
    1. public override void OnServerConnect(NetworkConnection conn)
    2. {
    3.       conn.connectionId; // <--- this is equal to connectionToClient.connectionID?
    4. }
     
    Last edited: Oct 2, 2015
  6. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Try comparing it:

    Code (CSharp):
    1. conn.connectionId.Equals(connectionToClient.connectionId)