Skip to main content

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

Last updated on October 24, 2024

Connect the UI to create and leave a session

  1. Open the SessionMenuHandler_Starter.cs file and add the following code:

    private SessionEssentialsWrapper_Starter sessionEssentialsWrapper;
  2. Add the following line to the Awake() function:

    private void Awake()
    {
    sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
    ...
    }
  3. Create new functions to bind and unbind the events from sessionEssentialsWrapper:

     private void SubcsribeSessionEvents()
    {
    sessionEssentialsWrapper.OnCreateSessionCompleteEvent += OnCreateSessionCompleted;
    sessionEssentialsWrapper.OnLeaveSessionCompleteEvent += OnLeaveSessionCompleted;
    }
    private void UnSubscribeSessionEvents()
    {
    sessionEssentialsWrapper.OnCreateSessionCompleteEvent -= OnCreateSessionCompleted;
    sessionEssentialsWrapper.OnLeaveSessionCompleteEvent -= OnLeaveSessionCompleted;
    }
  4. In the Start function, add the following code:

     if (sessionEssentialsWrapper == null)
    {
    sessionEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
    SubscribeSessionEvents();
    }
  5. Go to the OnEnable() function and update the code as follows:

    private void OnEnable()
    {
    CurrentView = SessionMenuView.Default;
    if (sessionEssentialsWrapper == null)
    {
    return;
    }

    SubscribeSessionEvents();
    }
  6. Navigate to the OnDisable() function and unsubscribe from the events with the following:

    private void OnDisable()
    {
    if (sessionEssentialsWrapper == null)
    {
    return;
    }

    UnSubscribeSessionEvents();
    }
  7. Update the OnEliminationButtonClicked() function. You need to define a variable to hold the value for SessionV2GameSessionCreateRequest, which will be passed to sessionEssentialsWrapper.CreateSession. The code below uses unity-elimination-none as the match pool which we can get from the GameSessionConfig class. The final code for this function will appear as follows:

    private void OnEliminationButtonClicked()
    {
    Dictionary<InGameMode,
    Dictionary<GameSessionServerType,
    SessionV2GameSessionCreateRequest>> sessionConfig = GameSessionConfig.SessionCreateRequest;

    if (!sessionConfig.TryGetValue(InGameMode.None, out var matchTypeDict))
    {
    BytewarsLogger.LogWarning("InGame Mode Not found");
    return;
    }

    if (!matchTypeDict.TryGetValue(GameSessionServerType.None, out var request))
    {
    BytewarsLogger.LogWarning("GameSessionServerType Not found");
    return;
    }

    CurrentView = SessionMenuView.Creating;
    Button button = creatingPanel.gameObject.GetComponentInChildren<Button>();
    button.gameObject.SetActive(true);
    sessionEssentialsWrapper.CreateSession(request);
    }
  8. In the OnLeaveSessionButtonClicked() function, you will implement logic to leave a session. Replace the existing implementation with the code below:

    private void OnLeaveSessionButtonClicked()
    {
    sessionEssentialsWrapper.LeaveSession(cachedSessionId);
    }
  9. Update the functions that are declared as the event listeners. There are two functions: OnCreateSessionCompleted() and OnLeaveSessionCompleted().

    private void OnCreateSessionCompleted(Result<SessionV2GameSession> result)
    {
    if (!result.IsError)
    {
    CurrentView = SessionMenuView.Joining;
    StartCoroutine(DelayCallback(sessionView => Helper(result, sessionView)));
    }
    else
    {
    CurrentView = SessionMenuView.Failed;
    }
    }
    private void OnLeaveSessionCompleted(Result<SessionV2GameSession> result)
    {
    if (!result.IsError)
    {
    MenuManager.Instance.OnBackPressed();
    }
    else
    {
    BytewarsLogger.LogWarning($"{result.Error.Message}");
    MenuManager.Instance.OnBackPressed();
    }
    }
  10. Creating a session will automatically join the session, so DelayCallback has been added as the delay to show the message from creating a session panel to joining a session, and then join the session.

Resources