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

すべてを統合する - 専用サーバーで参加可能なセッション - (Unreal Engine モジュール)

Last updated on October 23, 2024

Connect Create Match Session menu to the create, leave, and display current session status

  1. Open the CreateMatchSessionDSWidget_Starter CPP file and navigate to the CreateSession function. Add a call to the create session function from the Online Session. Note that the parent widget stores what game mode the player has selected. Then, retrieve that selected game mode and pass it to the Online Session.

    void UCreateMatchSessionDSWidget_Starter::CreateSession() const
    {
    if (OnlineSession->ValidateToStartSession.IsBound() &&
    !OnlineSession->ValidateToStartSession.Execute())
    {
    return;
    }

    // Make sure the retry and cancel game session is performed by this class when the DS network type is selected.
    W_Parent->GetProcessingWidgetComponent()->OnCancelClicked.Clear();
    W_Parent->GetProcessingWidgetComponent()->OnRetryClicked.Clear();
    W_Parent->GetProcessingWidgetComponent()->OnCancelClicked.AddUObject(this, &ThisClass::CancelJoiningSession);
    W_Parent->GetProcessingWidgetComponent()->OnRetryClicked.AddUObject(this, &ThisClass::CreateSession);

    W_Parent->SetLoadingMessage(TEXT_REQUESTING_SESSION_CREATION, false);
    W_Parent->SwitchContent(UCreateMatchSessionWidget::EContentType::LOADING);

    OnlineSession->CreateMatchSession(
    OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
    EGameModeNetworkType::DS,
    W_Parent->GetSelectedGameModeType());
    }
  2. Navigate to the CancelJoiningSession function. Call LeaveSession to achieve the cancel joining effect. Technically, when this function is called, the player is already inside the session, but in this case, it is currently waiting for the dedicated server to be ready.

    void UCreateMatchSessionDSWidget_Starter::CancelJoiningSession() const
    {
    W_Parent->SetLoadingMessage(TEXT_LEAVING_SESSION, false);
    W_Parent->SwitchContent(UCreateMatchSessionWidget::EContentType::LOADING);

    OnlineSession->LeaveSession(
    OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
    }
  3. In previous modules, you prepared several callback functions. Those functions need to be bound to the delegate for them to work. In the CPP file, navigate to NativeOnActivated and replace it with the code below.

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

    // Get online session
    UOnlineSession* BaseOnlineSession = GetWorld()->GetGameInstance()->GetOnlineSession();
    if (!ensure(BaseOnlineSession))
    {
    return;
    }
    OnlineSession = Cast<UAccelByteWarsOnlineSessionBase>(BaseOnlineSession);

    // Get parent menu widget
    W_Parent = GetFirstOccurenceOuter<UCreateMatchSessionWidget>();
    if (!ensure(W_Parent))
    {
    return;
    }

    OnlineSession->GetOnCreateSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnCreateSessionComplete);
    OnlineSession->GetOnLeaveSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnCancelJoiningSessionComplete);
    OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->AddUObject(
    this, &ThisClass::OnSessionServerUpdateReceived);

    Btn_StartMatchSessionDS->OnClicked().AddUObject(this, &ThisClass::CreateSession);
    }
  4. Unbind them when they're not needed. Navigate to NativeOnDeactivated and replace it with the code below.

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

    OnlineSession->GetOnCreateSessionCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->RemoveAll(this);

    Btn_StartMatchSessionDS->OnClicked().RemoveAll(this);
    W_Parent->GetProcessingWidgetComponent()->OnRetryClicked.RemoveAll(this);
    W_Parent->GetProcessingWidgetComponent()->OnCancelClicked.RemoveAll(this);
    }

Connect Browse Match menu to the find and join available sessions, and display current session status

  1. Open the BrowseMatchDSWidget_Starter CPP file and navigate to the FindSessions function. Call the FindSessions function from Online Session. Note that, if you want to increase the number of maximum sessions that the backend will return, simply increase the SessionsNumToQuery value in the Header file.

    void UBrowseMatchDSWidget_Starter::FindSessions(const bool bForce) const
    {
    W_Parent->SetLoadingMessage(TEXT_LOADING_DATA, true, false);
    W_Parent->SwitchContent(UBrowseMatchWidget::EContentType::BROWSE_LOADING);
    Btn_Refresh->SetIsEnabled(false);

    OnlineSession->FindSessions(
    OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
    SessionsNumToQuery,
    bForce);
    }
  2. Navigate to the CancelJoining function and replace it with the code below. Just like in the Create Match menu, this function will call LeaveSession.

    void UBrowseMatchDSWidget_Starter::CancelJoining() const
    {
    W_Parent->SetLoadingMessage(TEXT_LEAVING_SESSION, false, false);
    W_Parent->SwitchContent(UBrowseMatchWidget::EContentType::JOIN_LOADING);

    OnlineSession->LeaveSession(
    OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
    }
  3. Go to the JoinSession function. Replace it with the code below to call the corresponding Online Session function:

    void UBrowseMatchDSWidget_Starter::JoinSession(const FOnlineSessionSearchResult& SessionSearchResult) const
    {
    if (OnlineSession->ValidateToJoinSession.IsBound() &&
    !OnlineSession->ValidateToJoinSession.Execute(SessionSearchResult))
    {
    return;
    }

    W_Parent->SetLoadingMessage(TEXT_JOINING_SESSION, false, false);
    W_Parent->SwitchContent(UBrowseMatchWidget::EContentType::JOIN_LOADING);

    OnlineSession->JoinSession(
    OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
    OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
    SessionSearchResult);
    }
  4. Bind the callback functions to the callback delegates. In the CPP file, navigate to the NativeOnActivated and replace it with the code below:

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

    // Get online session
    UOnlineSession* BaseOnlineSession = GetWorld()->GetGameInstance()->GetOnlineSession();
    if (!ensure(BaseOnlineSession))
    {
    return;
    }
    OnlineSession = Cast<UAccelByteWarsOnlineSessionBase>(BaseOnlineSession);
    ensure(OnlineSession);

    // Get parent menu widget
    W_Parent = GetFirstOccurenceOuter<UBrowseMatchWidget>();
    if (!ensure(W_Parent))
    {
    return;
    }

    OnlineSession->GetOnLeaveSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnCancelJoiningComplete);
    OnlineSession->GetOnFindSessionsCompleteDelegates()->AddUObject(this, &ThisClass::OnFindSessionComplete);
    OnlineSession->GetOnJoinSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnJoinSessionComplete);
    OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->AddUObject(this, &ThisClass::OnSessionServerUpdateReceived);

    Btn_Refresh->OnClicked().AddUObject(this, &ThisClass::FindSessions, true);
    W_Parent->GetJoiningWidgetComponent()->OnCancelClicked.AddUObject(this, &ThisClass::CancelJoining);

    FindSessions(false);
    }
  5. Implement a way to properly unbind those callbacks. Navigate to NativeOnDeactivated and replace it with the code below:

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

    OnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnFindSessionsCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnJoinSessionCompleteDelegates()->RemoveAll(this);
    OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->RemoveAll(this);

    Btn_Refresh->OnClicked().RemoveAll(this);
    W_Parent->GetJoiningWidgetComponent()->OnCancelClicked.RemoveAll(this);
    }
  6. Compile your project and make sure there are no errors.

Upload dedicated server

To complete and play test this tutorial, you need to upload a new dedicated server image to the AccelByte Gaming Services (AGS) Admin Portal. To do this, refer back to the Dedicated servers with AccelByte Multiplayer Servers (AMS) module.

Resources