Skip to main content

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

Last updated on June 11, 2024

Connect the UI to the create, leave, and display current session status

  1. Open the project in Unity and open the CreateMatchSessionHandler_Starter script. You will call the AccelByte Gaming Services (AGS) Game SDK for Unity API to create a match session via the wrapper class. Before you can start the API call, you need to define the wrapper first. Add the code bellow to the CreateMatchSessionHandler_Starter file.

    private MatchSessionDSWrapper_Starter matchSessionDSWrapper_Starter;
  2. Then, add the code below into Start().

    private void Start()
    {
    matchSessionDSWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();

    //... some code omitted for brevity
    }
  3. Modify the CreateMatchSession function as follows:

    private void CreateMatchSession()
    {
    ShowLoading("Creating Match Session...", CancelCreateMatch);
    matchSessionDSWrapper_Starter.Create(gameMode,
    selectedSessionServerType, OnCreatedMatchSession);
    }
  4. You need to define the CancelCreateMatch function. This function will cancel the match session creation using the starter version of MatchSessionWrapper class.

    private void CancelCreateMatch()
    {
    if(shownRectTransform!=null)
    shownRectTransform.gameObject.SetActive(true);
    loadingPanel.gameObject.SetActive(false);
    matchSessionDSWrapper_Starter.CancelCreateMatchSession();
    }
  5. You still need to define the OnCreatedMatchSession function to handle the match creation result.

    private static void OnCreatedMatchSession(string errorMessage)
    {
    instance.dsBtn.interactable = true;
    if (!String.IsNullOrEmpty(errorMessage))
    {
    instance.ShowError(errorMessage);
    }
    else
    {
    //show loading, MatchSessionWrapper_Starter will move UI to lobby when connected to DS
    instance.ShowLoading("Joining Session");
    Debug.Log($"success create session");
    }
    }
  6. Play the game from Unity Editor. If successful, you will see this log:

     [MatchSessionDSWrapper_Starter.cs] [OnCreateGameSessionResult] [Log] [linenumber] - create session result:
  7. Congratulations! You successfully connected the UI with the wrapper class to create a match session. In the Play Test section, we will test create match session implementation.

Resources