Putting it all together - Introduction to Session - (Unreal Engine module)
Connect the UI to create, leave, and display current session status
Start by opening the
CreateSessionWidget_Starter
class Header file. Find the variable calledSessionTemplateName_Dummy
and fill it with the session template name you set up in Set up game sessions.protected:
// ...
const FString SessionTemplateName_Dummy = "unreal-elimination-none";You need to modify the function definition that you set up in Add session creation menu. Open
CreateSessionWidget_Starter
class CPP file and navigate toCreateSession
. Add the following code:void UCreateSessionWidget_Starter::CreateSession()
{
if (!SessionOnlineSession)
{
return;
}
// An event to validate to start the session will be used in the Play with Party module.
if (SessionOnlineSession->ValidateToStartSession.IsBound() &&
!SessionOnlineSession->ValidateToStartSession.Execute())
{
return;
}
Ws_Processing->LoadingMessage = TEXT_REQUESTING_SESSION_CREATION;
SwitchContent(EContentType::LOADING);
// Create dummy session. Override dummy session template name if applicable.
SessionOnlineSession->CreateSession(
SessionOnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
SessionOnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
FOnlineSessionSettings(),
EAccelByteV2SessionType::GameSession,
UTutorialModuleOnlineUtility::GetDummySessionTemplateOverride().IsEmpty() ?
SessionTemplateName_Dummy : UTutorialModuleOnlineUtility::GetDummySessionTemplateOverride());
}Still in the CPP file, navigate to
LeaveSession
and add 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));
}Bind the response function in this UI class to the Online Session response delegate. Navigate to
NativeOnActivated
and replace it with the code below. In this code, if the player is already in a game session, it immediately shows the widget's success state.void UCreateSessionWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
#pragma region "UI related"
Btn_Back->OnClicked().AddUObject(this, &ThisClass::DeactivateWidget);
Btn_CreateSession->OnClicked().AddUObject(this, &ThisClass::CreateSession);
Btn_Leave->OnClicked().AddUObject(this, &ThisClass::LeaveSession);
Ws_Processing->OnRetryClicked.AddUObject(this, &ThisClass::CreateSession);
#pragma endregion
SessionOnlineSession = Cast<UAccelByteWarsOnlineSessionBase>(GetGameInstance()->GetOnlineSession());
if (!ensure(SessionOnlineSession))
{
return;
}
SessionOnlineSession->GetOnCreateSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnCreateSessionComplete);
SessionOnlineSession->GetOnLeaveSessionCompleteDelegates()->AddUObject(this, &ThisClass::OnLeaveSessionComplete);
// Set initial UI state.
SwitchContent(EContentType::CREATE);
// Set UI state to success if already in a session.
const FName SessionName = SessionOnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession);
const FNamedOnlineSession* OnlineSession = SessionOnlineSession->GetSession(SessionName);
if (OnlineSession)
{
OnCreateSessionComplete(SessionName, true);
}
}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 following code:void UCreateSessionWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
#pragma region "UI related"
Btn_CreateSession->OnClicked().RemoveAll(this);
Btn_Back->OnClicked().RemoveAll(this);
Btn_Leave->OnClicked().RemoveAll(this);
Ws_Processing->OnRetryClicked.RemoveAll(this);
#pragma endregion
if (SessionOnlineSession)
{
SessionOnlineSession->GetOnCreateSessionCompleteDelegates()->RemoveAll(this);
SessionOnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
}
}
Resources
- The files used in this tutorial section are available in the Byte Wars Unreal GitHub repository.