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 = 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.
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;
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(SessionV2GameSessionCreateRequest request)
{
session.CreateGameSession(request, OnCreateSessionCompleted);
}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
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;
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);
}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
Similar to creating and joining sessions, you first need to define the event.
public event Action<Result<SessionV2GameSession>> OnLeaveSessionCompleteEvent;
Write a function to leave a session.
protected internal void LeaveSession(string sessionId)
{
session.LeaveGameSession(sessionId, OnLeaveSessionCompleted);
}The
LeaveSession()
function usessessionId
as a parameter to be passed to the SDK functionsession.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)
{
if (!result.IsError)
{
BytewarsLogger.Log($"Successfully left the game session");
}
else
{
BytewarsLogger.LogWarning($"Error : {result.Error.Message}");
}
OnLeaveSessionCompleteEvent?.Invoke(result);
}
Resources
- GitHub link to the files in the Unity Byte Wars repository: