Skip to main content

Connect a Game Client to Dedicated Servers in Multiple Regions

Last updated on
  1. Make sure the player is logged in.

  2. Use the GetServerLatencies function to get the latencies from each region. This will return an array of pairs of regions and latency in ms.

    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);
    }));
  3. Use the array of latencies when you call the StartMatchmaking function.

    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);
  4. Wait for the Lobby service to send a notification that a match has been found.

    Registry::Lobby.Connect();
    FRegistry::Lobby.SetMatchmakingNotifDelegate(AccelByte::Api::Lobby::MatchmakingNotif.CreateLambda([](const FAccelByteModelsMatchmakingNotice& Result)
    {
    if (Result.Status == EAccelByteMatchmakingStatus::Done)
    {
    FString MatchId = Result.MatchId;
    }
    }));
  5. Notify the game client that the player is ready to join the match using this function.

    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);
  6. Wait for the DS to get the match details from the Lobby service. Then, connect to the DS using those details.

    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;
    }));