メインコンテンツまでスキップ

Unreal Engine Module - Quick Match with DS - Putting it all together

Last updated on January 13, 2024
AGS Starter

This tutorial isn't yet applicable for the AccelByte Gaming Service (AGS) Starter tier. It requires the Armada, a dynamic game server manager feature, which isn't currently supported on AGS Starter.

Connecting UI with matchmaking with DS implementation

  1. Open the MatchmakingDSWidget_Starter CPP file. Then replace the StartMatchmaking() function using the following code to trigger start matchmaking. The OnlineSession variable is a reference to the online session you created earlier, which is the MatchmakingDSOnlineSession_Starter class.

    void UMatchmakingDSWidget_Starter::StartMatchmaking() const
    {
    // An event to validate the start of matchmaking, this will be used on playing with party module
    if (OnlineSession->ValidateToStartMatchmaking.IsBound() &&
    !OnlineSession->ValidateToStartMatchmaking.Execute(W_Parent->GetSelectedGameModeType()))
    {
    return;
    }

    // Otherwise, start matchmaking.
    W_Parent->SetLoadingMessage(TEXT_FINDING_MATCH, false);
    W_Parent->SwitchContent(UQuickPlayWidget::EContentType::LOADING);

    OnlineSession->StartMatchmaking(
    GetOwningPlayer(),
    OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
    EGameModeNetworkType::DS,
    W_Parent->GetSelectedGameModeType());
    }
  2. Next, replace the CancelMatchmaking() function using the following code to trigger cancel matchmaking.

    void UMatchmakingDSWidget_Starter::CancelMatchmaking() const
    {
    W_Parent->SetLoadingMessage(TEXT_CANCEL_MATCHMAKING, false);
    W_Parent->SwitchContent(UQuickPlayWidget::EContentType::LOADING);

    OnlineSession->CancelMatchmaking(
    GetOwningPlayer(),
    OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
    }
  3. Then, bind the callback functions to show relevant UIs when certain matchmaking events occur. To do this, add the following code to the NativeOnActivated() function.

    void UMatchmakingDSWidget_Starter::NativeOnActivated()
    {
    Super::NativeOnActivated();

    UOnlineSession* BaseOnlineSession = GetWorld()->GetGameInstance()->GetOnlineSession();
    if (!ensure(BaseOnlineSession))
    {
    return;
    }

    OnlineSession = Cast<UAccelByteWarsOnlineSessionBase>(BaseOnlineSession);
    ensure(OnlineSession);

    Btn_StartMatchmakingDS->OnClicked().AddUObject(this, &ThisClass::StartMatchmaking);

    W_Parent = GetFirstOccurenceOuter<UQuickPlayWidget>();
    if (!ensure(W_Parent))
    {
    return;
    }

    W_Parent->GetProcessingWidget()->OnCancelClicked.AddUObject(this, &ThisClass::CancelMatchmaking);
    W_Parent->GetProcessingWidget()->OnRetryClicked.AddUObject(this, &ThisClass::StartMatchmaking);

    // Bind events to listen to matchmaking callbacks.
    OnlineSession->GetOnStartMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnStartMatchmakingComplete);
    OnlineSession->GetOnCancelMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnCancelMatchmakingComplete);
    OnlineSession->GetOnMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnMatchmakingComplete);
    }
  4. Next, you need to unbind those callbacks when the widget is not active. In the NativeOnDeactivated() add the following code.

    void UMatchmakingDSWidget_Starter::NativeOnDeactivated()
    {
    Super::NativeOnDeactivated();

    Btn_StartMatchmakingDS->OnClicked().RemoveAll(this);

    W_Parent->GetProcessingWidget()->OnCancelClicked.RemoveAll(this);
    W_Parent->GetProcessingWidget()->OnRetryClicked.RemoveAll(this);

    // Unbind events from listening matchmaking callbacks.
    OnlineSession->GetOnStartMatchmakingCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnCancelMatchmakingCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnMatchmakingCompleteDelegates()->RemoveAll(this);
    }
  5. As mentioned in the previous section, there are several buttons you need to bind to let the player leave the game session. Those buttons are the leave button in the Match Lobby and the quit buttons in the Pause and Game Over widget. To do this, we can assign a delegate to leave the session. Open the MatchmakingDSOnlineSession_Starter CPP file and add the following code to the RegisterOnlineDelegates() function.

    void UMatchmakingDSOnlineSession_Starter::RegisterOnlineDelegates()
    {
    Super::RegisterOnlineDelegates();

    // Bind matchmaking delegates.
    GetSessionInt()->OnMatchmakingCompleteDelegates.AddUObject(this, &ThisClass::OnMatchmakingComplete);
    GetSessionInt()->OnCancelMatchmakingCompleteDelegates.AddUObject(this, &ThisClass::OnCancelMatchmakingComplete);
    GetABSessionInt()->OnBackfillProposalReceivedDelegates.AddUObject(this, &ThisClass::OnBackfillProposalReceived);

    // Bind server event delegates.
    GetABSessionInt()->OnSessionServerUpdateDelegates.AddUObject(this, &ThisClass::OnSessionServerUpdateReceived);
    GetABSessionInt()->OnSessionServerErrorDelegates.AddUObject(this, &ThisClass::OnSessionServerErrorReceived);

    // Bind buttons to leave the session.
    const TDelegate<void(APlayerController*)> LeaveSessionDelegate = TDelegate<void(APlayerController*)>::CreateWeakLambda(
    this, [this](APlayerController*)
    {
    LeaveSession(GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
    });
    UPauseWidget::OnQuitGameDelegate.Add(LeaveSessionDelegate);
    UMatchLobbyWidget::OnQuitLobbyDelegate.Add(LeaveSessionDelegate);
    UGameOverWidget::OnQuitGameDelegate.Add(LeaveSessionDelegate);
    }
  6. Finally, when the online session is deinitialized, you need to unbind to stop listening to the event. You can do this by adding the following code in the ClearOnlineDelegates() function.

    void UMatchmakingDSOnlineSession_Starter::ClearOnlineDelegates()
    {
    Super::ClearOnlineDelegates();

    // Unbind matchmaking delegates.
    GetSessionInt()->OnMatchmakingCompleteDelegates.RemoveAll(this);
    GetSessionInt()->OnCancelMatchmakingCompleteDelegates.RemoveAll(this);
    GetABSessionInt()->OnBackfillProposalReceivedDelegates.RemoveAll(this);

    // Unbind server event delegates.
    GetABSessionInt()->OnSessionServerUpdateDelegates.RemoveAll(this);
    GetABSessionInt()->OnSessionServerErrorDelegates.RemoveAll(this);

    // Unbind buttons from invoking leave session.
    UPauseWidget::OnQuitGameDelegate.RemoveAll(this);
    UMatchLobbyWidget::OnQuitLobbyDelegate.RemoveAll(this);
    UGameOverWidget::OnQuitGameDelegate.RemoveAll(this);
    }
  7. Congratulations! You have connected the UI with the DS matchmaking implementation.

Uploading dedicated server

To complete and play-test this tutorial, you need to upload a new dedicated server (DS) image to the Admin Portal. You can follow the steps you have learned from the previous module, the Module: Run a Dedicated Server on Armada or Module: Run a Dedicated Server on AccelByte Multiplayer Server (AMS).

Resources