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

Question Get SteamId from NetworkManager's side? Detect on Host if he lost internet connection?

Discussion in 'Netcode for GameObjects' started by Takii, Apr 25, 2023.

  1. Takii

    Takii

    Joined:
    Sep 5, 2017
    Posts:
    7
    Hey there. I am using Netcode for Gameobjects with FacepunchTransport and Facepunch.Steamworks.

    I've 2 main questions:

    1. How can I retrieve the SteamId in the OnClientConnectedCallback and the OnClientDisconnectedCallback? The Id's on the NetworkManager of course are no SteamId's but just 0(host), 1, 2, 3, ..... (clients). So i wonder if there is a proper way to retrive the clients SteamId over the NetworkManager, or if i have to manually assign it through the steam lobby?


    2. How can I detect on the Host side, if he lost Internet connection? For Clients the OnClientDisconnectedCallback Event will fire, but not for the Host himself. So whats the best way to detect that, in best case with the already given Tools/Features?


    Thats some code for reference, so you know what Events i am talking about & how i handle Host Disconnect detection for the Clients (also is the Host ALWAYS ID 0? Or should i better check with the OwnerID?):


    Code (CSharp):
    1. private void OnEnable() {
    2.     NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
    3.     NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectCallback;
    4. }
    5.  
    6. private void OnDisable() {
    7.     NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
    8.     NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectCallback;
    9. }
    10.  
    11. private void OnClientConnectCallback(ulong id) {
    12.     // How to get the SteamId?
    13. }
    14.  
    15. private void OnClientDisconnectCallback(ulong id) {
    16.     // How to get the SteamId?
    17.  
    18.     // +
    19.  
    20.     // How can I detect for the host himself if he lost internet connection? OnClientDisconnectCallback does not fire in that case at all
    21.  
    22.     Debug.Log("OnClientDisconnectCallback: " + id.ToString());
    23.  
    24.     if (IsClient) {
    25.         if (id == 0) { // 0 = host?!
    26.             LobbySaver.instance.currentLobby?.Leave();
    27.             LobbySaver.instance.currentLobby = null;
    28.             NetworkManager.Singleton.Shutdown();
    29.  
    30.             SceneManager.LoadScene("MainMenu");
    31.         }
    32.     }
    33. }
    Thanks in advance for any help!

    Greetings from Vienna & have a nice day :)
     
  2. fernando-a-cortez

    fernando-a-cortez

    Unity Technologies

    Joined:
    Dec 14, 2020
    Posts:
    11
    Hi,

    For number 1: there's no way to retrieve the SteamId of a user through NetworkManager. You'll have to manage that information yourself. Take a look at Boss Room's SessionManager for an example of handling a player's session data: https://github.com/Unity-Technologi....samples.coop/Utilities/Net/SessionManager.cs.

    For number 2: As of Netcode for GameObjects v1.4.0 (see release here: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/releases), a OnServerStarted and OnServerStopped callback have been introduced to determine when the server has been started or is no longer active.

    I hope that helps!
     
    RikuTheFuffs likes this.