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. Dismiss Notice

Resolved Join a client after host is started

Discussion in 'Multiplayer' started by JurgenvOorschot, Jul 16, 2023.

  1. JurgenvOorschot

    JurgenvOorschot

    Joined:
    Dec 1, 2018
    Posts:
    7
    I want to join a client after the host is started.
    I all seems to work when i join a client to a relay and then start the host (scenemanagers.networkload)
    But when the host is started it doesnst join.

    do i need to let the client join by loading the scene itself (doesnst seem to work)?

    Or is there a other way to let them join.

    The global idea is that a player can join and leave during the game and not use lobby for this.
    The relay code is stored in an firebase realtime database so no lobby needed to share the relaycode

    Hope someone can help
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,018
    The way you are stuck, I would suggest to not do scene loading right now. Get the hosting / joining running in a single scene first, then look into how you switch scenes, who does it when and how that syncs.
    Mixing both makes this case unnecessarily complex.
     
  3. JurgenvOorschot

    JurgenvOorschot

    Joined:
    Dec 1, 2018
    Posts:
    7
    In a normal way you get all clients started/joined. Then execute a scenemanager.networkload and all cliënten starts loadng the scène.

    But when the host is started it cannot load again cause the host and all clients start again
     
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    48
    @JurgenvOorschot
    It depends upon how you are synchronizing things when a client is connecting. You might read over this section of the documentation to see the two different client synchronization mode options: Client Synchronization Mode | Unity Multiplayer Networking



    I might be misunderstanding this, but here would be the general order of operations:
    1. You have two instances of the game running on the same device/computer or different ones
      1. Instance-A: Will be the host for the game session
      2. Instance-B: Will be the client that joins the session
    2. Instance-A starts a relay session and lobby for clients to join.
    3. Instance-B joins the relay session and lobby.
    4. Instance-A invokes NetworkManager.StartHost()
    5. Instance-B invokes NetworkManager.StartClient()
    6. Instance-A (Host) receives Instance-B connection request, approves the NGO client, and begins synchronizing the client (if scene management is enabled).
    7. Instance-B synchronizes based on the client synchronization and scene validation (if any) you have in place.
      1. When Instance-B has finished synchronizing it sends the SynchronizeComplete SceneEvent to the Host
    8. Instance-A receives Instance-B client's SynchronizeComplete message.

    At this point, if say you had a lobby scene where clients can connect prior to the host launching into any game-specific scene, you could have a button that becomes enabled/visible on the host side that lets the host "launch the game"... or select the scene/level to launch, etc.

    Here is a very high level order of operations:
    1. Connect to Relay and Lobby
    2. Start the NetworkManager (host first then any additional remote client joins the lobby and starts)
    3. Upon the first "remote client" connecting (i.e. NetworkManager.OnClientConnected is invoked on the host side) you could launch into a new scene (or the like).
      1. You could also just launch directly into a scene from the host side once the NetworkManager.Host has been started (after already having connected to relay and the lobby)
        1. The remote client, after invoking NetworkManager.StartClient, should load/synchronize the appropriate scenes (depends upon the client synchronization mode).
    Let me know if that helps you with your issue?