メインコンテンツまでスキップ

SDK を使用してマッチメイキングする - セッション入門 - (Unity モジュール)

Last updated on October 23, 2024

Unwrap the wrapper

In this section, you will learn how to create and leave a session using the AccelByte Gaming Services (AGS) Game SDK. Within the Byte Wars project, an SDK wrapper named SessionEssentialsWrapper.cs has been created. This C# class holds the session-related functionalities and will later be inherited by several other classes. In this tutorial, you will use a starter version of that wrapper so you can implement session functionalities from scratch.

What's in the Starter Pack

To follow this tutorial, a starter wrapper class called SessionEssentialsWrapper_Starter has been prepared for you and has several functionalities provided:

  • You will use Session, which contains session-related functions, and Lobby, which logs in to the lobby to access notifications. You can find these lines of code in SessionEssentialsWrapper_Starter.

    protected Session session;
    protected Lobby lobby;
  • In the Awake() function, Session and Lobby are defined using MultiRegistry to get them and store them to the variables _session and _lobby. It sets the access modifier for this function as protected to make it accessible from the inheritor class.

    protected void Awake()
    {
    session = AccelByteSDK.GetClientRegistry().GetApi().GetSession();
    lobby = AccelByteSDK.GetClientRegistry().GetApi().GetLobby();
    }

Implement create, join, and leave session

In this section, you will implement functionality to create, join, and leave a session using the AGS Game SDK.

Create Game Session

You can access any session-related functions from the session field that has the Session class type. To create a session, use the session.CreateGameSession function. You will use a class model called SessionRequestPayload as a parameter. It comes predefined so you can use it without any modification.

  1. Create an event that will be used in a callback function later. Open the SessionEssentialsWrapper_Starter.cs and add the code below.

    public event Action<Result<SessionV2GameSession>> OnCreateSessionCompleteEvent;
  2. Write a function to create a session. Open the SessionEssentialsWrapper_Starter.cs file and create a function called CreateSession. _session.CreateGameSession is already used in this function, which will call an API endpoint and send a request to the backend to create a game session.

    protected internal void CreateSession(SessionV2GameSessionCreateRequest request)
    {
    session.CreateGameSession(request, OnCreateSessionCompleted);
    }
  3. Create a callback function to handle the result when the session creation is complete.

    private void OnCreateSessionCompleted(Result<SessionV2GameSession> result)
    {
    if (!result.IsError)
    {
    BytewarsLogger.Log($"Successfully created game session");
    }
    else
    {
    BytewarsLogger.LogWarning($"Error : {result.Error.Message}");
    }

    OnCreateSessionCompleteEvent?.Invoke(result);
    }

The callback function will trigger the OnCreateSessionCompleteEvent event and pass the result to the class that has subscribed to this event.

Join Game Session

  1. To address joining sessions, you need the session ID that was created before. Create an event for leaving a session. Underneath the OnCreateSessionCompleteEvent, put the code below.

    public event Action<Result<SessionV2GameSession>> OnJoinSessionCompleteEvent;
  2. Write a function for joining a session. You will use session.JoinGameSession from the AGS Game SDK.

    protected internal void JoinSession(string sessionId)
    {
    session.JoinGameSession(sessionId, OnJoinSessionCompleted);
    }
  3. Write a callback function to notify when the player joins.

    private void OnJoinSessionCompleted(Result<SessionV2GameSession> result)
    {
    if (!result.IsError)
    {
    BytewarsLogger.Log($"Successfully joined the game session");
    }
    else
    {
    BytewarsLogger.LogWarning($"Error : {result.Error.Message}");
    }

    OnJoinSessionCompleteEvent?.Invoke(result);
    }

Leave session

  1. Similar to creating and joining sessions, you first need to define the event.

    public event Action<Result<SessionV2GameSession>> OnLeaveSessionCompleteEvent;
  2. Write a function to leave a session.

    protected internal void LeaveSession(string sessionId)
    {
    session.LeaveGameSession(sessionId, OnLeaveSessionCompleted);
    }
  3. The LeaveSession() function uses sessionId as a parameter to be passed to the SDK function session.LeaveGameSession that calls the API to leave a session. As you can see, the function above has a callback function which requires the tutorial type similar to OnCreateSessionCompleted.

  4. Create a callback for LeaveSession(), which calls OnLeaveSessionCompleted.

    private void OnLeaveSessionCompleted(Result<SessionV2GameSession> result)
    {
    if (!result.IsError)
    {
    BytewarsLogger.Log($"Successfully left the game session");
    }
    else
    {
    BytewarsLogger.LogWarning($"Error : {result.Error.Message}");
    }

    OnLeaveSessionCompleteEvent?.Invoke(result);
    }

Resources