Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question 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:
    146
    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:
    146
    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:
    53
    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:
    146
    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:
    53
    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:
    146
    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!
     
  7. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    146
  8. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    146
    Have there been any advancements with regards to this? Basically, is it possible to have a loading screen with Netcode for GameObjects?
     
  9. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    300
    Short of functionality being added to Netcode's SceneManager to allow for this the only option I know of to achieve it is to handle the scene management yourself.
     
  10. Triarios

    Triarios

    Joined:
    Oct 29, 2014
    Posts:
    2
    I've just started trying out Netcode for GameObjects and I'm running into similar issues, and I'm wondering how I would address them even if I'm handling the scene management myself.

    I have a client joining the host after the game has already started, and the game world is procedural and modifiable (think Minecraft or Valheim) so the host needs to send some world data to the client before they can load into the gameplay scene and instantiate the world. But the moment the client connects, the networking system immediately instantiates the host's player (and presumably any other networked objects that have been created) while the client is still in the main menu and hasn't even started receiving the data needed to load the world yet. Also, I haven't gotten this far yet, but I'm guessing I'm going to have problems with the client receiving RPCs while they're in this loading state - the host chops down a tree and sends out a message to delete that tree to the client, but the client doesn't even have a world with trees in it yet to act on that message.

    The workarounds I can think of to address this seem weird and convoluted, which makes me suspect I'm not understanding how to use the system correctly.
     
  11. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    300
    You'll need to take control of spawning the objects to individual clients. I would advise having objects hidden from all clients on spawning then use NetworkShow and NetworkHide to show and hide them to a particular client as required. For the player object you'll probably want to disable auto spawning but it looks to be possible to hide it on auto spawn as well.

    You'll also need a mechanism of tracking what client is in which scene to determine what objects to spawn to them.

    I could be wrong but I don't think it's feasible to have a host when scene management is disabled as the host's scene would also contain other client's objects for their scenes. I guess it's possible but the host wouldn't realistically then be able to switch scenes while the game is running. To save those kind of hassles and to allow a game host to drop without ending the game I run it as server only so there is no host to worry about.
    .
     
  12. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    187
    What I do with my procedural stuff is that I have some GameObjects in the scene that do not hold any physical components (e.g. no terrain or trees) but only (a) a NetworkObject component and (b) a NetworkBehaviour script.

    In the NetworkBehaviour's OnNetworkSpawn() I check if the caller is a server, and if so, I generate the procedural objects according to the script's parameters. The procedural stuff is all parented under the object that spawned it, and not NetworkObjects themselves. Because they are not themselves NetworkObjects, and not scene objects, they are not replicated automatically.

    If the caller is not a server, I call a ServerRpc that sends (through a directed ClientRpc) the data needed to recreate the current state on the calling client.
     
  13. Triarios

    Triarios

    Joined:
    Oct 29, 2014
    Posts:
    2
    The NetworkShow/Hide calls (along with the CheckObjectVisibility delegate) seem to give me the control I need to keep the clients from blowing up on the main menu. Thanks for pointing me in the right direction.
     
  14. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,532
    Unless I am misunderstanding something here, I can see in the release notes that Netcode for GameObjects v1.0.0 back in October 2021 added additive scene workflows.
     
  15. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    300
    You can use additive scenes but I'm not sure how they would be of a benefit here.
     
unityunity