Unreal Engine Module - Create a Joinable Session Using a Dedicated Server - - Put it all together
This tutorial isn't applicable to the AccelByte Gaming Service (AGS) Starter tier. It requires the AccelByte Multiplayer Server (AMS) or Armada, which isn't currently supported on AGS Starter.
Connect Create Match Session menu with Online Session class
Open the
CreateMatchSessionDSWidget_Starter
CPP file and navigate toCreateSession
function. Add a call to the create session function from the Online Session. Note that the parent widget stores what game mode the player had selected. Then, retrieve that selected game mode and pass it to the Online Session.void UCreateMatchSessionDSWidget_Starter::CreateSession() const
{
...
W_Parent->SwitchContent(UCreateMatchSessionWidget::EContentType::LOADING);
OnlineSession->CreateMatchSession(
OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
EGameModeNetworkType::DS,
W_Parent->GetSelectedGameModeType());
}Navigate to the
CancelJoiningSession
function. CallLeaveSession
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 (DS) 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));
}In the previous submodule, you have 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 add these highlighted lines.void UCreateMatchSessionDSWidget_Starter::NativeOnActivated()
{
...
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);
...
}You also need to properly unbind them when they're not needed. To do so, navigate to
NativeOnDeactivated
and add these highlighted lines.void UCreateMatchSessionDSWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
OnlineSession->GetOnCreateSessionCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->RemoveAll(this);
Btn_StartMatchSessionDS->OnClicked().RemoveAll(this);
...
}After this, the Create Match Session menu is fully functioning.
Connect Browse Match menu with Online Session class
Open the
BrowseMatchDSWidget_Starter
CPP file and navigate to theFindSessions
function. Call theFindSessions
function from Online Session. Note that, if you want to increase the number of maximum sessions that the backend will return, simply increase theSessionsNumToQuery
value in the header file.void UBrowseMatchDSWidget_Starter::FindSessions(const bool bForce) const
{
...
Btn_Refresh->SetIsEnabled(false);
OnlineSession->FindSessions(
OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
SessionsNumToQuery,
bForce);
}Navigate to the
CancelJoining
function and add these highlighted lines. Just like in the Create Match menu, this function will callLeaveSession
.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));
}Go to the
JoinSession
function. Add these highlighted lines to call the corresponding Online Session function.void UBrowseMatchDSWidget_Starter::JoinSession(const FOnlineSessionSearchResult& SessionSearchResult) const
{
...
W_Parent->SwitchContent(UBrowseMatchWidget::EContentType::JOIN_LOADING);
OnlineSession->JoinSession(
OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
SessionSearchResult);
}Bind the callback functions to the callback delegates. In the CPP file, navigate to the
NativeOnActivated
and add these highlighted lines.void UBrowseMatchDSWidget_Starter::NativeOnActivated()
{
...
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);
...
}Implement a way to properly unbind those callbacks. Navigate to
NativeOnDeactivated
and add these highlighted lines.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);
}All done! Both of the menus are now connected to the Online Session. 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 DS image to the AGS Admin Portal. To learn how, see Module: Run a Dedicated Server on Armada or Module: Run a Dedicated Server on AccelByte Multiplayer Server (AMS).
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionDS/UI/CreateMatchSessionDSWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionDS/UI/CreateMatchSessionDSWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionDS/UI/BrowseMatchDSWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionDS/UI/BrowseMatchDSWidget_Starter.cpp