Search Unity

[Solved] Server not adding player for reconnecting client

Discussion in 'Multiplayer' started by Valtaroth, Jun 13, 2015.

  1. Valtaroth

    Valtaroth

    Joined:
    Jun 8, 2013
    Posts:
    8
    Hi everyone,

    I am currently diving into UNet with Unity 5.1 and have come across the following problem while implementing a lobby system based on the NetworkManager component.

    Whenever a client connects for the first time, the server automatically creates a player for it (because I have checked autoCreatePlayer). When he disconnects, he is removed properly. If the same client reconnects though, the server does not create another player for it. It does recognize the connection and sends a custom message to the reconnecting client, but OnServerAddPlayer is not being called and the server does not receive a message sent by the client anymore which I DID receive the first time.

    In order to fix this, I would like to uncheck autoCreatePlayer and do it myself, but I couldn't figure out which methods are actually called to create a player. Just to clarify, I am not using the NetworkLobbyManager because it feels like giving up too much control. I don't like using the NetworkManager either, but with my limited knowledge at this point in time I don't feel very confident avoiding the HLAPI altogether.

    TL;DR version: what is actually called in the background when autoCreatePlayer is checked to create a player on the server?

    Cheers,
    Korbinian
     
  2. samfarhan

    samfarhan

    Joined:
    Oct 14, 2012
    Posts:
    14
    Inside of your class which extends from the NetworkManager, add this:

    Code (CSharp):
    1. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2.     {
    3.         GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity;
    4.  
    5.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    6.     }
    To get a client to spawn:

    Code (CSharp):
    1. ClientScene.AddPlayer(client.connection, 0);
    Where client is the current NetworkClient instance in the NetworkManager, and 0 is the player controller ID. (in nearly every case this will be 0 unless you have split screen)
     
  3. Valtaroth

    Valtaroth

    Joined:
    Jun 8, 2013
    Posts:
    8
    Thank you, I was missing this call to manually add a player. Unfortunately the reconnection problem does not get fixed by this. Instead of using autoCreatePlayer I am now creating players using ClientScene.AddPlayer and handle the addition in OnServerAddPlayer like before, which again works the first time a client connects. If he disconnects and then reconnects, he still isn't being added again. I am calling ClientScene.RemovePlayer on disconnection as well.

    Does reconnection work for you?
     
  4. samfarhan

    samfarhan

    Joined:
    Oct 14, 2012
    Posts:
    14
    I'm also having similar issues where clients can't see players created before they joined. Scene objects aren't even activated on clients for me! I'd really like to see the NetworkManager source code so I can see what I'm overriding to break it.
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    There is a lobby component already.. The NetworkLobbyManager which is a specialized NetworkManager...
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    auto-add player causes a player to be added in OnClientSceneChanged and OnClientConnect.

    Code (CSharp):
    1.         public virtual void OnClientConnect(NetworkConnection conn)
    2.         {
    3.             if (string.IsNullOrEmpty(m_OnlineScene) || (m_OnlineScene == m_OfflineScene))
    4.             {
    5.                 ClientScene.Ready(conn);
    6.                 if (m_AutoCreatePlayer)
    7.                 {
    8.                     ClientScene.AddPlayer(0);
    9.                 }
    10.             }
    11.             else
    12.             {
    13.                 // player will be added when on-line scene finishes loading
    14.             }
    15.         }
    Code (CSharp):
    1.  
    2.         public virtual void OnClientSceneChanged(NetworkConnection conn)
    3.         {
    4.             // always become ready.
    5.             ClientScene.Ready(conn);
    6.  
    7.             if (!m_AutoCreatePlayer)
    8.             {
    9.                 return;
    10.             }
    11.  
    12.             bool addPlayer = false;
    13.             if (ClientScene.localPlayers.Count == 0)
    14.             {
    15.                 // no players exist
    16.                 addPlayer = true;
    17.             }
    18.  
    19.             bool foundPlayer = false;
    20.             foreach (var playerController in ClientScene.localPlayers)
    21.             {
    22.                 if (playerController.gameObject != null)
    23.                 {
    24.                     foundPlayer = true;
    25.                     break;
    26.                 }
    27.             }
    28.             if (!foundPlayer)
    29.             {
    30.                 // there are players, but their game objects have all been deleted
    31.                 addPlayer = true;
    32.             }
    33.             if (addPlayer)
    34.             {
    35.                 ClientScene.AddPlayer(0);
    36.             }
    37.         }
     
    wlwl2 and TheGreatJ like this.
  7. Valtaroth

    Valtaroth

    Joined:
    Jun 8, 2013
    Posts:
    8
    @seanr I am avoiding the NetworkLobbyManager because I have no way of knowing what's happening under the hood, as I stated in my first post.

    Also, adding players works fine if they connect for the first time. All following connections by the same client (because he disconnected and connected again) will not cause a player to be created, neither automatically nor by calling ClientScene.AddPlayer manually.
     
    wlwl2 likes this.
  8. guzu

    guzu

    Joined:
    Aug 24, 2012
    Posts:
    2
    I'm facing the same issue even when using the Network Manager HUD. Reconnecting a client always fail to spawn players even with the most basic setup.
     
  9. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    I suppose that problem has been fixed in the patch 1.
     
  10. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    Anyone facing the problem when Auto Create Player is unchecked and it stills spawning the Player Prefab for a new connection? Debug.Log(autoCreatePlayer) returns false. I did not override any OnConnect methods.
     
  11. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    I did solved by overriding and commenting the line

    Code (CSharp):
    1. public override void OnClientConnect(NetworkConnection conn){
    2.  
    3.         //ClientScene.Ready (conn);
    4.         ClientScene.AddPlayer (0);
    5. }
     
  12. Valtaroth

    Valtaroth

    Joined:
    Jun 8, 2013
    Posts:
    8
    Perfect, thanks for telling me this. I'll install the patch as soon as possible then.

    @darkmask58 Sorry for not answering sooner, I'm glad you could work it out yourself :)
     
    darkmask58 likes this.
  13. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Auto Add Player doesn't work. I don't see any overrides of the code mentionned above for the lobbyManager.cs and ClientScene.AddPlayer(0) doesn't work in OnClientConnect().

    I am really wondering at this point if all this part of the system actually works... the tank and asteroid demo only use the "Game Player Prefab", not the "Player Prefab" in the Spawn Info section, so I can't find any working implementation of this.

    Bye,

    Jean
     
  14. Kuutti_Sampotti

    Kuutti_Sampotti

    Joined:
    Aug 3, 2014
    Posts:
    1
    I noticed that by default the LobbyPlayer (Clone) object does not get parented to the NetworkLobbyManager GameObject in the scene hierarchy. It would seem that all lobby players needs to be parented to NetworkLobbyManager so that the Player prefab gets instantiated when changing to Play Scene