Skip to main content

Put it all together - Create joinable sessions with dedicated servers - (Unity module)

Last updated on November 25, 2024

Hook up the create, leave, and display session UI

In this tutorial, you will connect the UI to create a match session by using the MatchSessionDSWrapper_Starter class, which already incorporates the AGS Game SDK implementation. The CreateMatchSessionDSHandler_Starter class has been provided as a UI handler for this tutorial.

  1. Open the CreateMatchSessionDSHandler_Starter.cs and add the code below:

    private MatchSessionDSWrapper_Starter matchSessionDSWrapper;
  2. You will use matchSessionDSWrapper.CreateMatchSession to create a match session. Go to the ClickDedicatedServerButton and replace the code with this:

    public void ClickDedicatedServerButton()
    {
    InGameMode selectedGameMode = InGameMode.None;
    GameSessionServerType selectedSessionServerType =
    TutorialModuleManager.Instance.IsModuleActive(TutorialType.MultiplayerDSEssentials) ?
    GameSessionServerType.DedicatedServerAMS :
    GameSessionServerType.DedicatedServer;

    MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
    if (menu is MatchSessionServerTypeSelection serverTypeSelection)
    {
    selectedGameMode = serverTypeSelection.SelectedGameMode;
    InitWrapper();
    }
    else
    {
    BytewarsLogger.LogWarning("Current menu is not server type selection menu while try to create match with DS");
    return;
    }

    matchSessionDSWrapper.CreateMatchSession(selectedGameMode, selectedSessionServerType);
    ShowLoading(
    "Creating Match Session (Dedicated Server)...",
    "Creating Match Session (Dedicated Server) is timed out.",
    createMatchSessionTimeoutSec,
    CancelCreateMatch);
    }
  3. Go to InitWrapper and implement the following code within the function:

    private void InitWrapper()
    {
    if (matchSessionDSWrapper == null)
    {
    matchSessionDSWrapper = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();
    }

    BindMatchSessionEvent();

    MatchSessionServerTypeSelection.OnBackButtonCalled -= OnBackButtonFromServerSelection;
    MatchSessionServerTypeSelection.OnBackButtonCalled += OnBackButtonFromServerSelection;
    }
  4. Now, set up the event binding in the BindMatchSessionEvent function along with the unbinding in UnbindMatchSessionEvent.

    private void BindMatchSessionEvent()
    {
    matchSessionDSWrapper.BindEvents();
    matchSessionDSWrapper.BindMatchSessionDSEvents();
    matchSessionDSWrapper.OnCreatedMatchSession += OnCreatedMatchSession;
    matchSessionDSWrapper.OnJoinedMatchSession += OnJoinedMatchSession;
    matchSessionDSWrapper.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
    }
    private void UnbindMatchSessionEvent()
    {
    matchSessionDSWrapper.UnbindEvents();
    matchSessionDSWrapper.UnbindMatchSessionDSEvents();
    matchSessionDSWrapper.OnCreatedMatchSession -= OnCreatedMatchSession;
    matchSessionDSWrapper.OnJoinedMatchSession -= OnJoinedMatchSession;
    matchSessionDSWrapper.OnLeaveSessionCompleted -= UnbindMatchSessionEvent;
    }
  5. OnCreatedMatchSession is responsible for updating the loading status and showing the cancel button. This button uses CancelCreateMatch as a callback. To be able to cancel matchmaking, update the CancelCreateMatch function with the code below.

    private void CancelCreateMatch()
    {
    Reset();
    ShowInfo("Match session creation cancelled");
    matchSessionDSWrapper.OnCreateMatchCancelled?.Invoke();
    matchSessionDSWrapper.CancelCreateMatchSession();
    }
  6. Play the game from the Unity editor. If successful, you will see this log:

     [MatchSessionWrapper.cs] [OnCreateSessionCompleted] [Log] [linenumber] - Successfully created the match session

Congratulations! You successfully connected the UI with the wrapper class to create a match session.

Resources