Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to do host-migration?

Discussion in 'Netcode for GameObjects' started by OrangeSensei, Oct 24, 2022.

  1. OrangeSensei

    OrangeSensei

    Joined:
    May 10, 2020
    Posts:
    5
    The docs in https://docs.unity.com/lobby/host-migration.html are very unhelpful. I tried to do host migration to the best of my understanding but the host does not migrate from the host that is leaving to the remaining client.

    How does host migration actually works? I want one of the remaining players to be the new host and go on playing with the other players. I am trying to work with relay+lobby services for the connection handling while NGO for the main gameplay session! Is there a way to setup the host migration for NGO so the match continues even if host leaves the game? I tried making a host migration script from other forum but it just seems to transfer the host of lobby not for the NGO!


    public async static void doMigrate()
    {
    var updatedLobby = await Lobbies.Instance.GetLobbyAsync(_currentLobby.Id);
    var nm = NetworkManager.Singleton;
    if (nm.IsHost)
    {
    Debug.Log("I am the host, have to migrate host to someone else");
    Lobby shouldHaveDifferentHost = await Lobbies.Instance.UpdateLobbyAsync(updatedLobby.Id, new UpdateLobbyOptions()
    {
    HostId = updatedLobby.Players.Where(p => p.Id != updatedLobby.HostId).First().Id
    });
    Debug.Log($"Has the host id changed? {shouldHaveDifferentHost.HostId != updatedLobby.HostId}");
    await Lobbies.Instance.RemovePlayerAsync(updatedLobby.Id, updatedLobby.HostId);
    await Task.Delay(5000);//Waiting because host migration is not instantaneous
    Debug.Log("Waited for migration, time do disconnect from the game");
    nm.DisconnectClient(nm.LocalClientId);
    nm.Shutdown(true);
    Debug.Log("I am dead. Did the host migrate?");
    }
    }
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    645
    OrangeSensei likes this.
  3. OrangeSensei

    OrangeSensei

    Joined:
    May 10, 2020
    Posts:
    5
    So is there any alternative way to make the game still running? like what i thought was to keep track of the current game state and if the host disconnect, the above code of mine will get the new host and i could create new relay allocation and restart the game with the last state and sync it to other clients? rn i am just making a game with 2 player so this method is what i thought of!
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    645
    It sounds possible, if all clients are saving the game state. It might be simpler to wait on the host to restart the game and have them restore the game from their own saved game state.
     
    OrangeSensei likes this.
  5. OrangeSensei

    OrangeSensei

    Joined:
    May 10, 2020
    Posts:
    5
    That might work for the coop game but what i am working on is a competitive one so having the other client resume it sounds reasonable ig? But if you suggest this can work i will quickly start to implement it using this method https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/session-management that they used in the boss room sample!
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    645
    If all clients are saving game state then I don't a reason why one of them can't become host.

    For that Boss Room code keep in mind that's for reconnecting clients to a running game not for re-starting one so the solution you need won't be as straight forward.
     
  7. OrangeSensei

    OrangeSensei

    Joined:
    May 10, 2020
    Posts:
    5
    I am fully aware that the code is not for reconnections but i would be using the same way to keep and update the data as they used! Regarding the first line ig you mean to say is you dont have any issues with choosing of the host? if any doubt you have, i am choosing the host based on the second available player on the lobby thats it! I will update the progress here if you are interested!
     
    cerestorm likes this.
  8. xeibra

    xeibra

    Joined:
    Jun 24, 2023
    Posts:
    1
    Hi OrangeSensei,

    Did you figure out this method? I also looking for a solution for host-migration on NGO! Thanks.
     
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,078
  10. PSmith_ricardo

    PSmith_ricardo

    Joined:
    Apr 6, 2020
    Posts:
    2
    Filled out a new idea card as I don't see anything related to this in the Roadmap at all...
     
    Last edited: Oct 23, 2023
  11. arwe2793

    arwe2793

    Joined:
    Oct 18, 2012
    Posts:
    25
    hey did you figured out how to do host migration? im particulary interested how to give new LOBBY host role to new player after original host suddenly disconects, after solving this problem im want to implement last of host migration for relays
     
  12. arwe2793

    arwe2793

    Joined:
    Oct 18, 2012
    Posts:
    25
    hey im having issue with unity lobby. if lobby host sudenly leaves game, for example crashes or wifi disconnects. after 30second lobby is destroyed or marked as inactive, how can i detect if lobby host disconected and transfer host role to another client ?