すべてを統合する - ピアツーピアでのクイックマッチ - (Unreal Engine モジュール)
Connect the UI to start, cancel, and display the matchmaking status
Open the
MatchmakingP2PWidget_Starter
CPP file and replace theStartMatchmaking()
function using the code below to trigger starting matchmaking. TheOnlineSession
variable is a reference to the online session you created earlier, which is theMatchmakingP2POnlineSession_Starter
class.void UMatchmakingP2PWidget_Starter::StartMatchmaking() const
{
if (OnlineSession->ValidateToStartMatchmaking.IsBound() &&
!OnlineSession->ValidateToStartMatchmaking.Execute(W_Parent->GetSelectedGameModeType()))
{
return;
}
// Otherwise, start matchmaking.
W_Parent->SetLoadingMessage(TEXT_FINDING_MATCH, false);
W_Parent->SwitchContent(UQuickPlayWidget::EContentType::LOADING);
OnlineSession->StartMatchmaking(
GetOwningPlayer(),
OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession),
EGameModeNetworkType::P2P,
W_Parent->GetSelectedGameModeType());
}Replace the
CancelMatchmaking()
function using the following code to trigger the canceling matchmaking:void UMatchmakingP2PWidget_Starter::CancelMatchmaking() const
{
W_Parent->SetLoadingMessage(TEXT_CANCEL_MATCHMAKING, false);
W_Parent->SwitchContent(UQuickPlayWidget::EContentType::LOADING);
OnlineSession->CancelMatchmaking(
GetOwningPlayer(),
OnlineSession->GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
}Bind the callback functions to show the relevant user interface (UI) when certain matchmaking events occur. To do this, add the following code to the
NativeOnActivated()
function:void UMatchmakingP2PWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
UOnlineSession* BaseOnlineSession = GetWorld()->GetGameInstance()->GetOnlineSession();
if (!ensure(BaseOnlineSession))
{
return;
}
OnlineSession = Cast<UAccelByteWarsOnlineSessionBase>(BaseOnlineSession);
ensure(OnlineSession);
W_Parent = GetFirstOccurenceOuter<UQuickPlayWidget>();
if (!ensure(W_Parent))
{
return;
}
// Bind buttons.
Btn_StartMatchmakingP2P->OnClicked().AddUObject(this, &ThisClass::StartMatchmaking);
W_Parent->GetProcessingWidget()->OnCancelClicked.AddUObject(this, &ThisClass::CancelMatchmaking);
W_Parent->GetProcessingWidget()->OnRetryClicked.AddUObject(this, &ThisClass::StartMatchmaking);
// Bind events to listen to matchmaking callbacks.
OnlineSession->GetOnStartMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnStartMatchmakingComplete);
OnlineSession->GetOnCancelMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnCancelMatchmakingComplete);
OnlineSession->GetOnMatchmakingCompleteDelegates()->AddUObject(this, &ThisClass::OnMatchmakingComplete);
}Unbind those callbacks when the widget is not active. In the
NativeOnDeactivated()
, add the following code:void UMatchmakingP2PWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
// Unbind buttons.
Btn_StartMatchmakingP2P->OnClicked().RemoveAll(this);
W_Parent->GetProcessingWidget()->OnCancelClicked.RemoveAll(this);
W_Parent->GetProcessingWidget()->OnRetryClicked.RemoveAll(this);
// Unbind events from listening matchmaking callbacks.
OnlineSession->GetOnStartMatchmakingCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnCancelMatchmakingCompleteDelegates()->RemoveAll(this);
OnlineSession->GetOnMatchmakingCompleteDelegates()->RemoveAll(this);
}There are several buttons you need to bind to let the player leave the game session. Those buttons are the leave button in the Match Lobby and the quit buttons in the Pause and Game Over widget. To do this, we can assign a delegate to leave the session. Open the
MatchmakingP2POnlineSession_Starter
CPP file and add the following code to theRegisterOnlineDelegates()
function:void UMatchmakingP2POnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
// Bind matchmaking delegates.
GetSessionInt()->OnMatchmakingCompleteDelegates.AddUObject(this, &ThisClass::OnMatchmakingComplete);
GetSessionInt()->OnCancelMatchmakingCompleteDelegates.AddUObject(this, &ThisClass::OnCancelMatchmakingComplete);
GetABSessionInt()->OnBackfillProposalReceivedDelegates.AddUObject(this, &ThisClass::OnBackfillProposalReceived);
// Bind server event delegates.
GetABSessionInt()->OnSessionServerUpdateDelegates.AddUObject(this, &ThisClass::OnSessionServerUpdateReceived);
GetABSessionInt()->OnSessionServerErrorDelegates.AddUObject(this, &ThisClass::OnSessionServerErrorReceived);
// Bind buttons to leave the session.
const TDelegate<void(APlayerController*)> LeaveSessionDelegate = TDelegate<void(APlayerController*)>::CreateWeakLambda(
this, [this](APlayerController*)
{
LeaveSession(GetPredefinedSessionNameFromType(EAccelByteV2SessionType::GameSession));
});
UPauseWidget::OnQuitGameDelegate.Add(LeaveSessionDelegate);
UMatchLobbyWidget::OnQuitLobbyDelegate.Add(LeaveSessionDelegate);
UGameOverWidget::OnQuitGameDelegate.Add(LeaveSessionDelegate);
}When the online session is deinitialized, you need to unbind to stop listening to the event. You can do this by adding the following code in the
ClearOnlineDelegates()
function:void UMatchmakingP2POnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
// Unbind matchmaking delegates.
GetSessionInt()->OnMatchmakingCompleteDelegates.RemoveAll(this);
GetSessionInt()->OnCancelMatchmakingCompleteDelegates.RemoveAll(this);
GetABSessionInt()->OnBackfillProposalReceivedDelegates.RemoveAll(this);
// Unbind server event delegates.
GetABSessionInt()->OnSessionServerUpdateDelegates.RemoveAll(this);
GetABSessionInt()->OnSessionServerErrorDelegates.RemoveAll(this);
// Unbind buttons from invoking leave session.
UPauseWidget::OnQuitGameDelegate.RemoveAll(this);
UMatchLobbyWidget::OnQuitLobbyDelegate.RemoveAll(this);
UGameOverWidget::OnQuitGameDelegate.RemoveAll(this);
}
Resources
The files used in this tutorial section are available in the Unreal Byte Wars GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchmakingP2P/UI/MatchmakingP2PWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchmakingP2P/UI/MatchmakingP2PWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchmakingP2P/MatchmakingP2POnlineSession_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Play/MatchmakingP2P/MatchmakingP2POnlineSession_Starter.cpp