Connect a Game Client to Dedicated Servers in Multiple Regions
Last updated on
Make sure the player is logged in.
Use the GetServerLatencies function to get the latencies from each region. This will return an array of pairs of regions and latency in ms.
- Unreal Engine
- Unity
FRegistry::Qos.GetServerLatencies(THandler<TArray<TPair<FString, float>>>::CreateLambda([](const TArray<TPair<FString, float>>& Result)
{
// Do something if GetServerLatencies has been successful
TArray<TPair<FString, float>> AllLatencies = Result;
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if GetServerLatencies has an error
UE_LOG(LogTemp, Log, TEXT("Error GetServerLatencies, Error Code: %d Error Messsage: %s"), ErrorCode, *ErrorMessage);
}));AccelBytePlugin.GetQos().GetServerLatencies(result =>
{
if (result.IsError)
{
// Do something if GetServerLatencies has an error
Debug.Log($"Error GetServerLatencies, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if GetServerLatencies has been successful
Dictionary<string, int> allLatencies = result.Value;
}
});Use the array of latencies when you call the StartMatchmaking function.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetMatchmakingNotifDelegate(AccelByte::Api::Lobby::MatchmakingNotif.CreateLambda([](const FAccelByteModelsMatchmakingNotice& Result)
{
// Do something if SetMatchmakingNotifDelegate has been successful
}));
FRegistry::Lobby.SendStartMatchmaking(GameMode, "", "", Latencies);AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().StartMatchmaking(gameMode, "", latencies, result =>
{
if (result.IsError)
{
// Do something if StartMatchmaking has an error
Debug.Log($"Error StartMatchmaking, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if StartMatchmaking has been successful
}
});Wait for the Lobby service to send a notification that a match has been found.
- Unreal Engine
- Unity
Registry::Lobby.Connect();
FRegistry::Lobby.SetMatchmakingNotifDelegate(AccelByte::Api::Lobby::MatchmakingNotif.CreateLambda([](const FAccelByteModelsMatchmakingNotice& Result)
{
if (Result.Status == EAccelByteMatchmakingStatus::Done)
{
FString MatchId = Result.MatchId;
}
}));AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().MatchmakingCompleted += result =>
{
if (result.IsError)
{
// Do something if MatchmakingCompleted has an error
Debug.Log($"Error MatchmakingCompleted, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if MatchmakingCompleted has been successful
if (result.Value.status == "Done")
{
string matchId = result.Value.matchId;
}
}
};Notify the game client that the player is ready to join the match using this function.
- Unreal Engine
- Unity
FString MatchId = FString("SomeMatchId");
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetReadyConsentResponseDelegate(AccelByte::Api::Lobby::ReadyConsentResponse.CreateLambda([](const FAccelByteModelsReadyConsentRequest& Result)
{
if (Result.Code == "0")
{
// Do something if ReadyConsentResponseDelegate has been successful
}
else
{
// Do something if ReadyConsentResponseDelegate has an error
}
}));
FRegistry::Lobby.SendReadyConsentRequest(MatchId);string matchId = "SomeMatchId";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().ConfirmReadyForMatch(matchId, result =>
{
if (result.IsError)
{
// Do something if ConfirmReadyForMatch has an error
Debug.Log($"Error ConfirmReadyForMatch, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if ConfirmReadyForMatch has been successful
}
});Wait for the DS to get the match details from the Lobby service. Then, connect to the DS using those details.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetDsNotifDelegate(AccelByte::Api::Lobby::DsNotif.CreateLambda([](const FAccelByteModelsDsNotice& Result)
{
// Do something if SetDsNotifDelegate has been successful
FString IpAddress = Result.Ip;
int32 Port = Result.Port;
}));AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().DSUpdated += result =>
{
if (result.IsError)
{
// Do something if DSUpdated has an error
Debug.Log($"Error DSUpdated, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if DSUpdated has been successful
}
};