Put it all together - Quick match with dedicated servers - (Unity module)
Connect the UI to start, cancel, and display the matchmaking status
In this tutorial, you will call the dedicated server (DS) matchmaking function in the MatchmakingSessionDSWrapper_Starter
class from the MatchmakingSessionDSHandler_Starter
class.
Open
MatchmakingSessionDSHandler_Starter
. AddMatchmakingSessionDSWrapper_Starter
as a field that you will use to call the API.private MatchmakingSessionDSWrapper_Starter matchmakingSessionDSWrapper;
Modify
ClickDedicatedServerButton
as shown below. This code will prepare the AccelByte Gaming Services (AGS) Game SDK service wrapper and start matchmaking based on the match pool name.public void ClickDedicatedServerButton()
{
MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
if (menu is MatchmakingSessionServerTypeSelection serverTypeSelection)
{
InitWrapper();
selectedGameMode = serverTypeSelection.SelectedGameMode;
}
else
{
BytewarsLogger.LogWarning("Current menu is not server type selection menu while try to matchmaking with DS");
return;
}
switch (selectedGameMode)
{
case InGameMode.OnlineDeathMatchGameMode:
matchmakingSessionDSWrapper.StartDSMatchmaking(InGameMode.OnlineDeathMatchGameMode);
ShowLoading("Start Team Death Match (Dedicated Server)...",
"Start Team Death Match (Dedicated Server) is timed out",
matchmakingTimeoutSec);
break;
case InGameMode.OnlineEliminationGameMode:
matchmakingSessionDSWrapper.StartDSMatchmaking(InGameMode.OnlineEliminationGameMode);
ShowLoading("Start Elimination Match (Dedicated Server)...",
"Start Elimination Match (Dedicated Server) is timed out",
matchmakingTimeoutSec);
break;
default:
string errorMsg = $"No Dedicated Server Match Pool for {selectedGameMode}";
BytewarsLogger.LogWarning(errorMsg);
ShowError(errorMsg);
break;
}
}Go to
InitWrapper
and update the function using the code below:private void InitWrapper()
{
if (matchmakingSessionDSWrapper == null)
{
matchmakingSessionDSWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchmakingSessionDSWrapper_Starter>();
}
BindMatchmakingEvent();
MatchmakingSessionServerTypeSelection.OnBackButtonCalled -= OnBackButtonFromServerSelection;
MatchmakingSessionServerTypeSelection.OnBackButtonCalled += OnBackButtonFromServerSelection;
}Now, set up your event binding in the
BindMatchmakingEvent
function, along with the unbinding inUnbindMatchmakingEvents
.private void BindMatchmakingEvent()
{
if (matchmakingSessionDSWrapper == null)
{
return;
}
matchmakingSessionDSWrapper.BindMatchmakingEvent();
matchmakingSessionDSWrapper.OnMatchTicketDSCreated += OnMatchTicketDSCreated;
matchmakingSessionDSWrapper.OnMatchmakingWithDSMatchFound += OnMatchmakingWithDSMatchFound;
matchmakingSessionDSWrapper.OnMatchmakingWithDSTicketExpired += OnMatchmakingWithDSTicketExpired;
matchmakingSessionDSWrapper.OnMatchmakingWithDSJoinSessionStarted += OnMatchmakingWithDSJoinSessionStarted;
matchmakingSessionDSWrapper.OnMatchmakingWithDSJoinSessionCompleted += OnMatchmakingWithDSJoinSessionCompleted;
matchmakingSessionDSWrapper.OnMatchmakingWithDSCanceled += OnMatchmakingWithDSCanceled;
matchmakingSessionDSWrapper.OnMatchmakingWithDSError += ErrorPanel;
matchmakingSessionDSWrapper.OnMatchmakingError += ErrorPanel;
matchmakingSessionDSWrapper.OnDSError += ErrorPanel;
matchmakingSessionDSWrapper.OnIntentionallyLeaveSession += Reset;
matchmakingSessionDSWrapper.OnDSAvailable += Travelling;
matchmakingSessionDSWrapper.OnInvitedToSession += OnInvitedToGameSession;
matchmakingSessionDSWrapper.OnRejectGameSessionCompleteEvent += OnSessionRejected;
}private void UnbindMatchmakingEvents()
{
if (matchmakingSessionDSWrapper == null)
{
return;
}
matchmakingSessionDSWrapper.UnbindMatchmakingEvent();
matchmakingSessionDSWrapper.OnMatchTicketDSCreated -= OnMatchTicketDSCreated;
matchmakingSessionDSWrapper.OnMatchmakingWithDSMatchFound -= OnMatchmakingWithDSMatchFound;
matchmakingSessionDSWrapper.OnMatchmakingWithDSTicketExpired -= OnMatchmakingWithDSTicketExpired;
matchmakingSessionDSWrapper.OnMatchmakingWithDSJoinSessionStarted -= OnMatchmakingWithDSJoinSessionStarted;
matchmakingSessionDSWrapper.OnMatchmakingWithDSJoinSessionCompleted -= OnMatchmakingWithDSJoinSessionCompleted;
matchmakingSessionDSWrapper.OnMatchmakingWithDSCanceled -= OnMatchmakingWithDSCanceled;
matchmakingSessionDSWrapper.OnMatchmakingWithDSError -= ErrorPanel;
matchmakingSessionDSWrapper.OnMatchmakingError -= ErrorPanel;
matchmakingSessionDSWrapper.OnDSError -= ErrorPanel;
matchmakingSessionDSWrapper.OnIntentionallyLeaveSession -= Reset;
matchmakingSessionDSWrapper.OnDSAvailable -= Travelling;
matchmakingSessionDSWrapper.OnInvitedToSession -= OnInvitedToGameSession;
matchmakingSessionDSWrapper.OnRejectGameSessionCompleteEvent -= OnSessionRejected;
}You have
OnMatchTicketDSCreated
that is responsible for updating the loading and showing of the cancel button. This button usesCancelDSMatchmaking
as a callback. To be able to cancel matchmaking, update theCancelDSMatchmaking
function with the code below.private void CancelDSMatchmaking()
{
matchmakingSessionDSWrapper.CancelDSMatchmaking();
ShowLoading("Cancelling Match", "Cancelling match is timed out", cancelMatchmakingTimeoutSec);
}Once a match is found, the player can choose to accept or reject the game session. To enable this functionality, update the
AcceptMatch()
andRejectMatch()
functions with the code below:private void AcceptMatch()
{
matchmakingSessionDSWrapper.AcceptSessionInvitation();
ShowLoading("Joining Match", "Rejecting Match is timed out", cancelMatchmakingTimeoutSec);
}private void RejectMatch()
{
matchmakingSessionDSWrapper.RejectSessionInvitation();
ShowLoading("Rejecting Match", "Rejecting Match is timed out", cancelMatchmakingTimeoutSec);
}To allow players to leave a session due to loading timeout, go to
OnDSTimeOut
and update it with the code below:private void OnDSTimeOut()
{
matchmakingSessionDSWrapper.LeaveCurrentSession();
Reset();
}
Now, when you quick-play using the Elimination game mode using a dedicated server, the game will start dedicated server matchmaking via the MatchmakingSessionDSWrapper_Starter
class.
Resources
- The files used in this tutorial section is available in the ByteWars GitHub repository.