Skip to main content

Put it all together - Quick match with dedicated servers - (Unity module)

Last updated on November 25, 2024

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.

  1. Open MatchmakingSessionDSHandler_Starter. Add MatchmakingSessionDSWrapper_Starter as a field that you will use to call the API.

    private MatchmakingSessionDSWrapper_Starter matchmakingSessionDSWrapper;
  2. 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;
    }
    }
  3. 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;
    }
  4. Now, set up your event binding in the BindMatchmakingEvent function, along with the unbinding in UnbindMatchmakingEvents.

    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;
    }
  5. You have OnMatchTicketDSCreated that is responsible for updating the loading and showing of the cancel button. This button uses CancelDSMatchmaking as a callback. To be able to cancel matchmaking, update the CancelDSMatchmaking function with the code below.

    private void CancelDSMatchmaking()
    {
    matchmakingSessionDSWrapper.CancelDSMatchmaking();
    ShowLoading("Cancelling Match", "Cancelling match is timed out", cancelMatchmakingTimeoutSec);
    }
  6. Once a match is found, the player can choose to accept or reject the game session. To enable this functionality, update the AcceptMatch() and RejectMatch() 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);
    }
  7. 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