Skip to main content

Unreal Engine Module - Introduction to Multiplayer Session - Putting it all together

Last updated on January 13, 2024

Connecting create session menu with session essentials

  1. Start by opening CreateSessionWidget_Starter class header file. Find variable called SessionTemplateName_Dummy and fill it with the Session Template name that you have set up in the Setting up game sessions.

    protected:
    const FString SessionTemplateName_Dummy = "unreal-elimination-none";
  2. Now, modify the function definition that you have set up in the Adding UI section. Open CreateSessionWidget_Starter class CPP file and navigate to CreateSession. Add the highlighted lines in the following code.

    void UCreateSessionWidget_Starter::CreateSession()
    {
    if (!SessionOnlineSession)
    {
    return;
    }

    // An event to validate to start the session, will be used on playing with party module
    if (SessionOnlineSession->ValidateToStartSession.IsBound() &&
    !SessionOnlineSession->ValidateToStartSession.Execute())
    {
    return;
    }

    Ws_Processing->LoadingMessage = TEXT_REQUESTING_SESSION_CREATION;
    SwitchContent(EContentType::LOADING);

    SessionOnlineSession->CreateSession(
    SessionOnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
    SessionOnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
    FOnlineSessionSettings(),
    EAccelByteV2SessionType::GameSession,
    SessionTemplateName_Dummy);
    }
  3. Onward to the leave session function. Still in the CPP file, navigate to LeaveSession and add the highlighted lines in the following code.

    void UCreateSessionWidget_Starter::LeaveSession()
    {
    if (!SessionOnlineSession)
    {
    return;
    }

    Ws_Processing->LoadingMessage = TEXT_LEAVING_SESSION;
    SwitchContent(EContentType::LOADING);

    SessionOnlineSession->LeaveSession(
    SessionOnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
    }
  4. With the request functions implemented, now bind the response function in this UI class to the Online Session response delegate. Navigate to NativeOnActivated and add the highlighted lines in the following code.

    void UCreateSessionWidget::NativeOnActivated()
    {
    ...
    if (!ensure(SessionOnlineSession))
    {
    return;
    }

    SessionOnlineSession->GetOnCreateSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnCreateSessionComplete);
    SessionOnlineSession->GetOnLeaveSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnLeaveSessionComplete);

    ...
    }
  5. To make sure that the response functions in this menu won't trigger when the user is not in the menu, unbind the response functions from the response delegate when the UI closes. Still in the CPP file, navigate to NativeOnDeactivated and add the highlighted lines in the following code.

    void UCreateSessionWidget::NativeOnDeactivated()
    {
    ...

    if (SessionOnlineSession)
    {
    SessionOnlineSession->GetOnCreateSessionCompleteDelegates()->RemoveAll(this);
    SessionOnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
    }
    }
  6. There's a chance that when a player open the widget, they are already a part of a session. If that happens, we want the widget to be in the Success state. Navigate to NativeOnActivated and add the highlighted line in the following code.

    void UCreateSessionWidget::NativeOnActivated()
    {
    ...
    const FName SessionName = SessionOnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession);
    const FNamedOnlineSession* OnlineSession = SessionOnlineSession->GetSession(SessionName);
    if (OnlineSession)
    {
    OnCreateSessionComplete(SessionName, true);
    }
    }
  7. With that, you are done! Your UI is now connected to the Session functionalities. Compile your project again and make sure there are no errors. Once that's done, move to the next section to test your implementation.

Resources