Auto Host Migration
Key Problem
- how to handle host disconnection problem.
- define multiple practices that can be done to address this cases
- Handle on player/room leader disconnect
- New Issue: Host is transferred, but player id is also changed, ex: Player 1 becomes Player 2, Player 2 becomes Player 3, etc Need to handle manually to give back the correct State Authority
Expected Output/Definition of Done
UploadAble Into Progresstransfer host to another player and continue the game as previous state
Key Solution
To just transfer the Host (Host Migration), Fusion already provides an Auto Host Migration, you just need to enable and set the configuration from Network Project Config.
Host Migration Snapshot Interval is delay in seconds between each time the Host send the simulation data to be used in Host Migration, so it’s expected some of the previous simulation data is missing because it has not been sent yet.
You need also implement some script in INetworkCallbacks to start the new Host and transferred the simulation data.
public async void OnHostMigration(NetworkRunner runner, HostMigrationToken hostMigrationToken)
{
// Shutdown the previous Runner
await runner.Shutdown(shutdownReason: ShutdownReason.HostMigration);
// Create a new Runner, you could also use a prefab
InstantiateRunner(GameMode.AutoHostOrClient);
// Start a new runner using Host Migration Token
StartGameResult result = await _runner.StartGame(new StartGameArgs
{
ObjectPool = _pool,
SceneObjectProvider = _sceneProvider,
HostMigrationToken = hostMigrationToken,
HostMigrationResume = OnHostMigrationResume
});
}
private void OnHostMigrationResume(NetworkRunner runner)
{
// Used to get the previous simulation data, to get player back in current state
// But the problem is, the player id is confused, need to handle it manually (WIP)
var resumedNetObjs = runner.GetResumeSnapshotNetworkObjects();
}