The docs in Host migration 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?");
}
}
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!
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.
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!
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.
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!
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
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 ?