Search Unity

Who disconnected?!

Discussion in 'Multiplayer' started by mojtaba64, Mar 7, 2017.

  1. mojtaba64

    mojtaba64

    Joined:
    Aug 3, 2010
    Posts:
    56
    Hi,

    I'm working on a 2 player online racing game.
    I've implemented all OnXXX function in my CustomNetworkManager including OnClientDisconnect and OnServerDisconnect. I ran my game on different computers and disconnected cable on server and client. No matter who disconnected form the game, OnClientDisconnect called on client and OnServerDisconnect called on server...

    So I can't say Player X disconnected So He/She is the loser and the other player is the winner!

    See below code, no matter server or host disconnected, I've got same prints in my console (on server)

    Code (CSharp):
    1. public override void OnServerDisconnect(NetworkConnection conn)
    2.     {
    3.         //base.OnServerDisconnect(conn);
    4.         NetworkServer.DestroyPlayersForConnection(conn);
    5.  
    6.         print(conn.address);
    7.         print(conn.clientOwnedObjects);
    8.         print(conn.connectionId);
    9.         print(conn.hostId);
    10.         print(conn.isConnected);
    11.         print(conn.isReady);
    12.         print(conn.lastError);
    13.         print(conn.lastMessageTime);
    14.         print(conn.logNetworkMessages);
    15.         print(conn.playerControllers);
    16.     }
    It seems that there was a function in legacy unity network that don't work in new UNET:
    https://docs.unity3d.com/ScriptReference/Network.OnDisconnectedFromServer.html
     
  2. HaqerzMer

    HaqerzMer

    Joined:
    Feb 22, 2015
    Posts:
    4
    Before calling NetworkServer.DestroyPlayerForConnection(conn), you can access the disconnecting player's gameobject with this:

    conn.playerControllers[0].gameobject;
     
  3. mojtaba64

    mojtaba64

    Joined:
    Aug 3, 2010
    Posts:
    56
    Thanks, but it won't work either.
    I put the destroy line to the end of the function.

    In OnServerDisconnect "conn.playerControllers[0].gameobject" always return the client (player who joined the host), no matter who disconnected.
    In OnClientDisconnect "conn.playerControllers[0].gameobject" always return itself (that joined to host), no matter who disconnected.

    I've searched a lot and pretty sure there is no way to find out which player disconnected :(
     
  4. HaqerzMer

    HaqerzMer

    Joined:
    Feb 22, 2015
    Posts:
    4
    It works, I am using it on a lot of my projects.

    So let's say you have a "PlayerScript" class in the PlayerPrefab and has a [SyncVar] string PlayerName;


    so on your OnServerDisconnect...


    public override void OnServerDisconnect(NetworkConnection conn)
    {
    // This will display the value of the variable PlayerName in the Server's console window
    Debug.Log(conn.playerControllers[0].gameobject.GetComponent<PlayerScript>().PlayerName);
    string tmpName = conn.playerControllers[0].gameobject.GetComponent<PlayerScript>().PlayerName;
    // If you want to relay the message to all other clients, call a RPC
    RpcBroadcastmessage(tmpName + " has disconnected the game."); // Only a sample RPC call
    NetworkServer.DestroyPlayersForConnection(conn);
    }
     
  5. mojtaba64

    mojtaba64

    Joined:
    Aug 3, 2010
    Posts:
    56
    I think your projects have more than 2 players and you always get the object of the CLIENT player that joined the host. Yeah, that is no problem, you can always get that player and send it's name with rpc or whatever your want. But, what happens if the internet of the host disconnect??
    OnServerDisconnect will call for each non local player and you can't understand that it was the the clinets that disconnected or the host...

    This line of your code shows that you didn't get whats my problem:
    RpcBroadcastmessage(tmpName + " has disconnected the game."); // Only a sample RPC call
    If Host system disconnected it just send the name of clients that joined the host to the client on the host system (there is no connection that could call rpc on client systems...) and it doesn't show that it was the client system that disconnected or the host...

    Actually OnServerDisconnect can be used if you always be sure that the host system don't disconnect...
     
  6. mojtaba64

    mojtaba64

    Joined:
    Aug 3, 2010
    Posts:
    56
    For more explanation, OnServerDisconnect correctly send the client that disconnected from the host, but it just show that client disconnected from the host, it doesn't show that the problem was from the host internet connection or the client internet connection...
     
  7. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    I am not quite sure how you expect to know that anyway. You can tell the connection dropped but what if it was a node between the host and the client that went kaput, neithers fault. Internet is internet. If the host/server disconnects easy to know cause everyone will splat.