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

すべてを統合する - 専用サーバーでのクイックマッチ - (Unreal Engine モジュール)

Last updated on May 30, 2024

Connect the UI to start, cancel, and display the matchmaking status

  1. Open the MatchmakingDSWidget_Starter CPP file and replace the StartMatchmaking() function using the code below to trigger the start of 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. Replace the CancelMatchmaking() function using the following code to trigger the canceling of 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. Bind the callback functions to show the relevant user interface 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. You need to unbind those callbacks when the widget is not active. In 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. 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, you can assign a delegate to leave the session. Open the MatchmakingDSOnlineSession_Starter CPP file and add the following code to RegisterOnlineDelegates():

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

Upload dedicated server

To complete and play-test this tutorial, you need to upload a new dedicated server image to the Admin Portal. You can redo the steps from the Dedicated servers with AccelByte Multiplayer Server (AMS) module to do this.

Resources