Search Unity

Bug Network disconnection + reconnection when host is alone vs multiple clients connected

Discussion in 'Multiplayer' started by DoctorTM, Mar 23, 2024.

  1. DoctorTM

    DoctorTM

    Joined:
    Feb 22, 2014
    Posts:
    3
    Hi,

    I have these 2 scenarios in Netcode. One works, the other doesn't. I can't figure out what's the difference between the two.

    Scenario 1 (working):
    Client A (C-A) opens a host. Client B (C-B) connects to C-A lobby. C-A loads the game, everything works. C-A shutdowns the server. C-A opens a new host. C-B reconnects. Everything works.



    Scenario 2 (Not working):
    C-A opens a host. C-B connects to C-A. C-B leaves. C-A closes the host. C-A opens new host. C-B connects to C-A lobby. C-A sees the connection, starts the game (from lobby). C-A doesn't see C-B's actions anymore. Everyone else in the game can see each others movement, actions, calls, but the host (C-A).
    If C-B opens a host, C-A still can't see any of the actions from other players. While other players see each others actions (including C-A's).
    Even if C-A opens a host, and a different client connects to it (not C-B). C-A still won't see anything. Also server scripts are not running (coroutines that run on the server aren't running).




    Any idea what could be the issue?

    One assumption that I have, is Netcode has different shutdown protocols when there's someone connected or not.
     
    Last edited: Mar 23, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,012
    Nope.

    That's on you, almost 100% guarantee. ;)

    This is common for networking. One of the very first things you should do in a new network project after you get the connection handling done, is to test all combinations of host, join, leave, disconnect and then re-running any of these in any combination. There's always something that goes wrong, some field isn't getting cleared, or previous run's reference remains, or you are either not registering a network callback the second time, or you register it a second time so it runs twice. Things like that.

    Without seeing any code we can only speculate. Which I do assuming you have just this one scene, right? That may be an issue. I always make a point of using a 3 scene setup: bootstrap, offline and online. Easiest to work with and avoid any issues.

    Bootstrap contains all persistent (don't destroy on load) objects like NetworkManager or multiplayer tools (monitor, simulator) and any of your own. You always enter playmode through that scene (I have a helper that enters playmode in that scene regardless of active scene).

    Offline is where the game is started, where the menu goes. As soon as server or host starts, the online scene is single-loaded and connected clients follow that. They can also join later and be moved to the online scene. When a client leaves, or the server quits, they single load the offline scene again. It's not a requirement but I like to start with a clean slate when going either online or offline.
     
    DoctorTM likes this.
  3. DoctorTM

    DoctorTM

    Joined:
    Feb 22, 2014
    Posts:
    3
    That was my initial assumption, that some data is being preserved between plays. It's just weird to me that from a multi client connection, closing the lobby works fine. And a single client connection - closing the lobby messes it up.

    Applying your design (offline, bootstrap, online) to my case would be: Offline is the main menu, intermediate bootstrap that player doesn't see, and online would be the lobby and game itself. The scene logic would be:
    1. main menu - player chooses to join/create lobby
    2. bootstrap - has the network manager, and takes care of connections
    3. Lobby and gameplay.
    4. and once the player is done playing, I'd clean up (destroy) any don't destroy on load network related objects - effectively loading the main menu (offline scene) clean with no multi related logic.
    Did I miss something?