Skip to main content

Put it all together - Introduction to Session - (Unity module)

Last updated on July 28, 2025

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

  1. Open the CreateSessionMenu_Starter class and define a variable to store the wrapper you created.

    private SessionEssentialsWrapper_Starter sessionEssentialsWrapper;
  2. Replace the CreateSession() function with the code below. This function sends a request to create a new session. The session request details such as the session template name, joinability, and type are retrieved from the GetGameSessionRequestModel(), a helper function from the AccelByteWarsOnlineSessionModels class.

    private void CreateSession()
    {
    SessionV2GameSessionCreateRequest request =
    AccelByteWarsOnlineSessionModels.GetGameSessionRequestModel(InGameMode.None, GameSessionServerType.None);
    if (request == null)
    {
    widgetSwitcher.ErrorMessage = InvalidSessionTypeMessage;
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
    return;
    }

    widgetSwitcher.LoadingMessage = CreatingSessionMessage;
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);

    sessionEssentialsWrapper.CreateGameSession(request, OnCreateSessionComplete);
    }
  3. Create a new callback function to handle when the session is created. This function displays the created session ID and switches the menu to the success state.

    private void OnCreateSessionComplete(Result<SessionV2GameSession> result)
    {
    if (result.IsError)
    {
    widgetSwitcher.ErrorMessage = result.Error.Message;
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
    return;
    }

    sessionIdText.text = result.Value.id;
    createSessionPanel.gameObject.SetActive(false);
    sessionResultPanel.gameObject.SetActive(true);
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
    }
  4. Replace the LeaveSession() function with the code below. This function sends a request to leave the current active session.

    private void LeaveSession()
    {
    widgetSwitcher.LoadingMessage = LeavingSessionMessage;
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);

    sessionEssentialsWrapper.LeaveGameSession(sessionIdText.text, OnLeaveSessionComplete);
    }
  5. Create the function below to handle when the leave session request is complete. This function switches the menu back to the default state to display the create session button.

    private void OnLeaveSessionComplete(Result result)
    {
    if (result.IsError)
    {
    widgetSwitcher.ErrorMessage = result.Error.Message;
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
    return;
    }

    createSessionPanel.gameObject.SetActive(true);
    sessionResultPanel.gameObject.SetActive(false);
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
    }
  6. Finally, replace the OnEnable() function with the code below. It initializes the wrapper and displays the last session if any.

    private void OnEnable()
    {
    // Reset to default state.
    createSessionPanel.gameObject.SetActive(true);
    sessionResultPanel.gameObject.SetActive(false);
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);

    sessionEssentialsWrapper ??= TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
    if (!sessionEssentialsWrapper)
    {
    return;
    }

    // Show the last session result if any.
    if (AccelByteWarsOnlineSession.CachedSession != null)
    {
    OnCreateSessionComplete(Result<SessionV2GameSession>.CreateOk(AccelByteWarsOnlineSession.CachedSession));
    }
    }

Resources