Search Unity

Help Wanted Joining mid-game from a MainMenu scene?

Discussion in 'Netcode for GameObjects' started by daniel_lochner, Jun 23, 2021.

  1. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    111
    Hi there,

    In my game, players join from a MainMenu scene, and can enter a hosted game at any time (i.e., players do not necessarily connect at the same time).

    This means that when a client successfully connects to a hosted game, all networked objects are immediately replicated over the network, and so are instantiated in the MainMenu scene! When the scene then changes to the LoadingScreen, these objects are destroyed.

    This issue can be quickly solved by invoking
    DontDestroyOnLoad(gameObject);
    in the object’s method once instantiated, however this feels sloppy, as players and game-specific scene objects shouldn’t be in the MainMenu and LoadingScreen scenes.

    What I would instead like to do is postpone spawning over the network for the client connecting, until they have entered the Game scene. Is this possible?

    Thanks for your time!
     
  2. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    111
    I’m currently spawning all NetworkObjects in the MainMenu scene, and setting them as invisible until they’ve reached the Game scene. Does anyone possibly have a better suggestion? Thanks!
     
  3. Todiloo

    Todiloo

    Joined:
    Jan 22, 2016
    Posts:
    42
    Look at the Boss example. It does late spawning of Player object, both from a lobby scene as well as joining late to a game in progress. (You can find the relevant code in ServerBossRoomState.cs)
     
  4. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    111
    The BossRoom example project loads the game scene immediately for returning players, which is strange, as it means you can't select your character. This is a perfect example of the issue I am facing.

    In order for the player to select an available character, they would first need to connect to the game. Once connected, all networked objects are immediately replicated over the network, and so these objects would then be instantiated in the "CharSelect" scene.

    They've overcome this issue by just randomly assigning the connecting player an available character, and also not include a loading scene (resulting in the game appearing unresponsive for quite a while).
     
  5. Todiloo

    Todiloo

    Joined:
    Jan 22, 2016
    Posts:
    42
    MLAPI can't do additive scenes which I guess would be a good fit here.
    https://unity.com/roadmap/unity-platform/multiplayer-networking
    its currently under "Planned"
     
  6. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    111
    I'm currently achieving this by placing networked objects in DontDestroyOnLoad.

    But I'm not quite sure if this will fix the issue though. We'd need a method for delayed "spawning". For example; once the player's connection is approved, we could pass a parameter through the connection approval callback to specify whether or not networked objects should all be replicated over the network. If false, we would be required to invoke a method on the client to send an Rpc to the server to request that all networked objects be spawned (i.e., once the game scene is fully loaded). Would this be possible @TwoTen? I can spend a bit of time this weekend if you think it is worth looking into? Thanks!
     
unityunity