Use the SDK for matchmaking - Introduction to Session - (Unity module)
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, andLobby
, 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
andLobby
are defined usingMultiRegistry
to get them and store them to the variables_session
and_lobby
. It sets the access modifier for this function asprotected
to make it accessible from the inheritor class.protected void Awake()
{
_session = MultiRegistry.GetApiClient().GetSession();
_lobby = MultiRegistry.GetApiClient().GetLobby();
}In
Start()
,LoginToLobby
is assigned to the login completing event.private void Start()
{
LoginHandler.onLoginCompleted += LoginToLobby;
}
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 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.
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<SessionResponsePayload> OnCreateSessionCompleteEvent;
Write a function to create a session. Open the
SessionEssentialsWrapper_Starter.cs
file and create a function calledCreateSession
._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(SessionRequestPayload sessionRequest, [CallerFilePath] string? sourceFilePath=null)
{
var gameSessionRequest = SessionUtil.CreateGameSessionRequest(sessionRequest);
var tutorialType = SessionUtil.GetTutorialTypeFromClass(sourceFilePath);
_session.CreateGameSession(gameSessionRequest, result => OnCreateSessionCompleted(result, tutorialType));
}As mentioned,
SessionRequestPayload
is used as a parameter, and[CallerFilePath] string? sourceFilePath=null
is also used. The second parameter is to get the file path of the caller class; leave this parameter as empty.To establish a session with
_session.CreateGameSession
, you need to provide an argument of typeSessionV2GameSessionCreateRequest
. A utility class,SessionUtil
, has been created to map aSessionRequestPayload
object toSessionV2GameSessionCreateRequest
.Create a callback function to handle the result when the session creation is complete.
private void OnCreateSessionCompleted(Result<SessionV2GameSession> result, TutorialType? tutorialType = null)
{
var response = new SessionResponsePayload();
if (!result.IsError)
{
BytewarsLogger.Log($"Successfully created game session");
response.Result = result;
response.TutorialType = tutorialType;
}
else
{
BytewarsLogger.LogWarning($"{result.Error.Message}");
response.IsError = result.IsError;
}
OnCreateSessionCompleteEvent?.Invoke(response);
}
The callback function will trigger the OnCreateSessionCompleteEvent
event and pass the result to the class that has subscribed to this event. The object type of this event is SessionResponsePayload
, which is a class model that wraps the result obtained from the API response and includes the TutorialType
enum. This enum will later serve as a flag to filter for the event subscribers.
Join Session
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<SessionResponsePayload> OnJoinSessionCompleteEvent;
Write a function for joining a session. You will use
_session.JoinGameSession
from the AGS Game SDK.protected void JoinSession(string sessionId, [CallerFilePath] string? sourceFilePath=null)
{
var tutorialType = SessionUtil.GetTutorialTypeFromClass((sourceFilePath));
_session.JoinGameSession(sessionId, result => OnJoinSessionCompleted(result, tutorialType));
}Write a callback function to notify when the player joins.
private void OnJoinSessionCompleted(Result<SessionV2GameSession> result, TutorialType? tutorialType = null)
{
var response = new SessionResponsePayload();
if (!result.IsError)
{
BytewarsLogger.Log($"Successfully joined the game session");
response.Result = result;
response.TutorialType = tutorialType;
}
else
{
BytewarsLogger.LogWarning($"{result.Error.Message}");
response.IsError = result.IsError;
}
OnJoinSessionCompleteEvent?.Invoke(response);
}
Leave session
Similar to creating and joining sessions, you first need to define the event.
public event Action<SessionResponsePayload> OnLeaveSessionCompleteEvent;
Write a function to leave a session.
protected internal void LeaveSession(string sessionId, [CallerFilePath] string? sourceFilePath=null)
{
var tutorialType = SessionUtil.GetTutorialTypeFromClass(sourceFilePath);
_session.LeaveGameSession(sessionId, result => OnLeaveSessionCompleted(result, tutorialType));
}The
LeaveSession()
function usessessionId
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 toOnCreateSessionCompleted
.Create a callback for
LeaveSession()
, which callsOnLeaveSessionCompleted
.private void OnLeaveSessionCompleted(Result<SessionV2GameSession> result, TutorialType? tutorialType = null)
{
var response = new SessionResponsePayload();
if (!result.IsError)
{
BytewarsLogger.Log($"Successfully left the game session");
response.Result = result;
response.TutorialType = tutorialType;
}
else
{
BytewarsLogger.LogWarning($"{result.Error.Message}");
response.IsError = result.IsError;
}
OnLeaveSessionCompleteEvent?.Invoke(response);
}
Resources
- GitHub link to the files in the Unity Byte Wars repository: