Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[UNET] Strange work of OnClientConnect

Discussion in 'UNet' started by slakdevelopacc, Oct 21, 2016.

  1. slakdevelopacc

    slakdevelopacc

    Joined:
    Jan 5, 2016
    Posts:
    14
    Hi everyone! I want to call a function ClientScene.AddPlayer with a version in which the three parameters (that send extra message). But, i really do not understand how this thing (UNET) - works.
    My unity version is 5.4.1f1.
    I open the documentation and see that:
    • NetworkManager.OnClientConnect - Called on the client when connected to a server. The default implementation of this function sets the client as ready and adds a player.

    • NetworkManager.OnClientSceneChanged -Called on clients when a scene has completed loaded, when the scene load was initiated by the server. Scene changes can cause player objects to be destroyed. The default implementation of OnClientSceneChanged in the NetworkManager is to add a player object for the connection if no player object exists.
    And also on this (link) page of docs u can see that:
    Code (CSharp):
    1. // called when connected to a server
    2. public virtual void OnClientConnect(NetworkConnection conn)
    3. {
    4.     ClientScene.Ready(conn);
    5.     ClientScene.AddPlayer(0);
    6. }
    7.  
    OK! So, let's try to do same by override this func?
    Code (CSharp):
    1. public class MyNetworkManager : NetworkManager {
    2.  
    3.     public override void OnClientConnect(NetworkConnection conn)
    4.     {
    5.         Debug.Log("OnClientConnect Called");
    6.  
    7.         // use "base.OnClientConnect(conn)" does not change anything
    8.  
    9.         ClientScene.Ready(conn);
    10.         ClientScene.AddPlayer(0);
    11.     }
    12.  
    13.  
    14.     public override void OnClientSceneChanged(NetworkConnection conn)
    15.     {
    16.         Debug.Log("OnClientSceneChanged Called");
    17.  
    18.         base.OnClientSceneChanged(conn);
    19.     }
    20.  
    21.     // .............
    But, when I start to play (no matter - host or remote client), Unity informs me:

    2016-10-21_23-54-22.png

    Wat? I thought maybe in the example, not all the contents of a virtual function OnClientConnect...
    But, how i can override this??? How i can call another version of ClientScene.AddPlayer?
    Okay, I see that there is a problem in ClientSceneChanged. But docs tell:
    So, why ClientSceneChanged want set connection to READY? Ok, let's try to delete whole code from OnClientConnect:
    Code (CSharp):
    1. public class MyNetworkManager : NetworkManager {
    2.  
    3.     public override void OnClientConnect(NetworkConnection conn)
    4.     {
    5.         Debug.Log("OnClientConnect Called");
    6.     }
    7.  
    8.     public override void OnClientSceneChanged(NetworkConnection conn)
    9.     {
    10.         Debug.Log("OnClientSceneChanged Called");
    11.  
    12.         base.OnClientSceneChanged(conn);
    13.     }
    14.  
    15.     // .............
    Errors are gone (all works fine)... Wat?

    Then I tried to do so:
    Code (CSharp):
    1. public class MyNetworkManager : NetworkManager {
    2.  
    3.     public override void OnClientConnect(NetworkConnection conn)
    4.     {
    5.         Debug.Log("OnClientConnect Called");
    6.     }
    7.  
    8.     public override void OnClientSceneChanged(NetworkConnection conn)
    9.     {
    10.         Debug.Log("OnClientSceneChanged Called");
    11.  
    12.         ClientScene.Ready(conn);
    13.         ClientScene.AddPlayer(0);
    14.     }
    15.  
    16.     // .............
    It's also work fine... But if i add one string in OnClientSceneChanged:
    Code (CSharp):
    1.  public override void OnClientSceneChanged(NetworkConnection conn)
    2.     {
    3.         Debug.Log("OnClientSceneChanged Called");
    4.  
    5.         base.OnClientSceneChanged(conn);
    6.  
    7.         ClientScene.Ready(conn);
    8.         ClientScene.AddPlayer(0);
    9.     }
    Again generated with the same erorrs...
    What a hell is going on? HELP.
     
    Last edited: Oct 21, 2016
  2. slakdevelopacc

    slakdevelopacc

    Joined:
    Jan 5, 2016
    Posts:
    14
    I dug a little in the sources (UnityEngine.Networking) and figured out that the "Ready\AddPlayer" is set mainly on scene change, provided that the manager will be checked AutoCreatePlayer.

    From sources (5.4):
    Code (CSharp):
    1. public virtual void OnClientConnect(NetworkConnection conn)
    2.         {
    3.             if (!clientLoadedScene)
    4.             {
    5.                 // Ready/AddPlayer is usually triggered by a scene load completing. if no scene was loaded, then Ready/AddPlayer it here instead.
    6.                 ClientScene.Ready(conn);
    7.                 if (m_AutoCreatePlayer)
    8.                 {
    9.                     ClientScene.AddPlayer(0);
    10.                 }
    11.             }
    12.         }
    13.  
    14. public virtual void OnClientSceneChanged(NetworkConnection conn)
    15.         {
    16.             // always become ready.
    17.             ClientScene.Ready(conn);
    18.  
    19.             if (!m_AutoCreatePlayer)
    20.             {
    21.                 return;
    22.             }
    23.  
    24.             //..........
    25.  
    26.        }
    I hope this helps someone some day.
     
  3. Deleted User

    Deleted User

    Guest

    Thanks, It helped me a lot!
     
    slakdevelopacc likes this.