すべてを統合する - ピアツーピアで参加可能なセッション - (Unreal Engine モジュール)
Connect Create Match Session menu to the create, leave, and display current session status
Begin by opening the
CreateMatchSessionP2PWidget_Starter
CPP file and navigate to theCreateSession
function. Add a call to the create session function from the Online Session. Remember that the parent widget stores what game mode the player has selected, hence, we retrieve that selected game mode and pass it to the Online Session.void UCreateMatchSessionP2PWidget_Starter::CreateSession() const
{
...
W_Parent->SwitchContent(UCreateMatchSessionWidget::EContentType::LOADING);
OnlineSession->CreateMatchSession(
OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
EGameModeNetworkType::P2P,
W_Parent->GetSelectedGameModeType());
}Navigate to
CancelJoiningSession
function. Here, we simply callLeaveSession
to cancel joining. Since we are in a peer-to-peer (P2P) session, there is a chance that, upon joining, the session info already includes the host IP address. In that case, the UI won't let the player cancel the session. Otherwise, the game will wait to receive updates from the backend, and the game will let the player cancel.void UCreateMatchSessionP2PWidget_Starter::CancelJoiningSession() const
{
W_Parent->SetLoadingMessage(TEXT_LEAVING_SESSION, false);
W_Parent->SwitchContent(UCreateMatchSessionWidget::EContentType::LOADING);
OnlineSession->LeaveSession(
OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
}You have prepared several callback functions previously. Those need to be bound to the delegate in order for them to work. Still in the CPP file, navigate to
NativeOnActivated
and add these highlighted lines:void UCreateMatchSessionP2PWidget_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_StartMatchSessionP2P->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 UCreateMatchSessionP2PWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
OnlineSession->GetOnCreateSessionCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnLeaveSessionCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnSessionServerUpdateReceivedDelegates()->RemoveAll(this);
Btn_StartMatchSessionP2P->OnClicked().RemoveAll(this);
...
}
Connect Browse Match menu to the find, join, and display current session status
Open the
BrowseMatchP2PWidget_Starter
CPP file and navigate toFindSessions
function. Here, you need to call theFindSessions
function from Online Session. Note that, if you want to increase the number of maximum sessions that the backend will return, you can increase theSessionsNumToQuery
value in the Header file.void UBrowseMatchP2PWidget_Starter::FindSessions(const bool bForce) const
{
...
Btn_Refresh->SetIsEnabled(false);
OnlineSession->FindSessions(
OnlineSession->GetLocalUserNumFromPlayerController(GetOwningPlayer()),
SessionsNumToQuery,
bForce);
}Navigate to the
CancelJoining
function and add the highlighted lines below. Just like in the Create Match menu, this function will simply callLeaveSession
.void UBrowseMatchP2PWidget_Starter::CancelJoining() const
{
W_Parent->SetLoadingMessage(TEXT_LEAVING_SESSION, false, false);
W_Parent->SwitchContent(UBrowseMatchWidget::EContentType::JOIN_LOADING);
OnlineSession->LeaveSession(
OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
}Navigate to the
JoinSession
function and add these highlighted lines to call the corresponding Online Session function:void UBrowseMatchP2PWidget_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. Still in the CPP file, navigate to
NativeOnActivated
and add these highlighted lines:void UBrowseMatchP2PWidget_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 UBrowseMatchP2PWidget_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);
}Compile your project and make sure there are no errors.
Resources
- The files used in this tutorial section are available in the Unreal Byte Wars GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionP2P/UI/CreateMatchSessionP2PWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionP2P/UI/CreateMatchSessionP2PWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionP2P/UI/BrowseMatchP2PWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchSessionP2P/UI/BrowseMatchP2PWidget_Starter.cpp