Hello everyone,
I’m taking my first steps to create a host migration system, which is crucial for my game as it operates as a death match. In other words, if a player loses, they are automatically disconnected. If that player happens to be the host, they should pass the host role to another client. Imagine my surprise when I discovered that Unity Networking does not support host migration. Now that I’ve practically finished the entire game, I’m feeling a bit desperate, haha! I’ve attached my two scripts where I handle all the lobby and relay processing, and this is my host migration function. Unfortunately, I’ve been working on this for two weeks now and can’t seem to get it to work. For now, I just want to do something very simple: only the host can change roles and make one of the other clients a host. Could you please help me figure out what I’m doing wrong and give me some guidance on how to fix it so I can release my game?
Thank you.
public static async Task MigrateHost()
{
Debug.LogError("Migrating host from 1");
// Check if there's a valid lobby and if the player is the current host
if (_currentLobby != null && _currentLobby.HostId == Authentication.PlayerId)
{
Debug.LogError("Migrating host from 2");
// Select a new player as the host, if there are other players in the lobby
if (NetworkManager.Singleton.ConnectedClientsList.Count > 1)
{
// Select a new host randomly from the connected clients list
var newHostClientId = ulong.MinValue; // Placeholder value for new host's client ID
foreach (var client in NetworkManager.Singleton.ConnectedClientsList)
{
// Check if the client is not the current host
if (client.ClientId != NetworkManager.Singleton.LocalClientId)
{
newHostClientId = client.ClientId;
break;
}
}
// Check if a new host was found
if (newHostClientId != ulong.MinValue)
{
// Get the PlayerId of the new host from the lobby players list
var newHost = _currentLobby.Players.FirstOrDefault(player => player.Id == Authentication.PlayerId);
if (newHost != null)
{
Debug.LogError($"Migrating host from {Authentication.PlayerId} to {newHost.Id}");
// Relay initialization
Debug.LogError("Starting a new Relay allocation...");
// Create a new Relay allocation
var newAllocation = await RelayService.Instance.CreateAllocationAsync(_currentLobby.MaxPlayers);
Debug.LogError($"New Relay allocation created. Allocation ID: {newAllocation.AllocationId}");
// Update the lobby with the new host and the new Relay connection data
await Lobbies.Instance.UpdateLobbyAsync(_currentLobby.Id, new UpdateLobbyOptions
{
HostId = newHost.Id
});
Debug.LogError($"Host migrated to player {newHost.Id}. Relay connection data updated.");
// Reset the transport with the new Relay connection data
Transport.SetHostRelayData(newAllocation.RelayServer.IpV4, (ushort)newAllocation.RelayServer.Port, newAllocation.AllocationIdBytes, newAllocation.Key, newAllocation.ConnectionData);
Debug.LogError("Relay connection data set in the transport. Host migration complete.");
// Get all clients connected to the previous host
var connectedClients = NetworkManager.Singleton.ConnectedClientsList;
// Disconnect each client from the previous host and connect them to the new host
foreach (var client in connectedClients)
{
// Disconnect the client from the previous host
NetworkManager.Singleton.DisconnectClient(client.ClientId);
// Wait for a short period to ensure the client is properly disconnected
await Task.Delay(500);
// Assign the new host as the owner of the player object of each client
if (client.PlayerObject != null)
{
client.PlayerObject.ChangeOwnership(newHostClientId);
}
else
{
Debug.LogError($"Player object not found for client {client.ClientId}");
}
}
Debug.LogError("All clients migrated to the new host.");
// Initialize the new host
NetworkManager.Singleton.StartHost();
Debug.LogError("New host initialized.");
}
else
{
Debug.LogError("Failed to find the new host in the lobby players list.");
}
}
else
{
Debug.LogError("Failed to find a new host from the connected clients list.");
}
}
else
{
Debug.LogError("No other players in the lobby to become the host");
}
}
else
{
Debug.LogError("You are not the current host or there is no valid lobby");
}
}
9765375–1398909–LobbyOrchestrator.cs (6.04 KB)
9765375–1398912–MatchmakingService.cs (10.1 KB)