Use the SDK for managing session - Introduction to Session - (Unity module)
Unwrap the wrapper
In this tutorial, you will learn how to implement AccelByte Gaming Services (AGS) Session using the AGS Software Development Kit (SDK). In Byte Wars, a game instance wrapper is defined in the SessionEssentialsWrapper
class. This wrapper manages session-related actions such as creating, leaving, and handling session invitations. For this tutorial, you will use the SessionEssentialsWrapper_Starter
class, which is a starter version of the SessionEssentialsWrapper
.
What's in the Starter Pack
To follow this tutorial, you will use the starter wrapper class called SessionEssentialsWrapper_Starter
. This class inherits from the AccelByteWarsOnlineSession
class, which is defined in the following file:
- C# file:
Assets/Resources/Modules/Play/OnlineSessionUtils/Scripts/AccelByteWarsOnlineSession.cs
The SessionEssentialsWrapper_Starter
class is defined in the file below:
- C# file:
Assets/Resources/Modules/Play/SessionEssentials/Scripts/SessionEssentialsWrapper_Starter.cs
Additionally, there are model classes containing helper functions and variables, defined in the following files:
- C# file:
Assets/Resources/Modules/Play/OnlineSessionUtils/Scripts/AccelByteWarsOnlineSessionModels.cs
- C# file:
Assets/Resources/Modules/Play/SessionEssentials/Scripts/SessionEssentialsModels.cs
The AccelByteWarsOnlineSession
class is a base abstract class for session-related logic. It contains several predefined helper components, including:
-
Helper variables for referencing AGS SDK interfaces. These variables are assigned during initialization:
protected static User User;
protected static Lobby Lobby;
protected static Session Session;
protected virtual void Awake()
{
...
User ??= AccelByteSDK.GetClientRegistry().GetApi().GetUser();
Lobby ??= AccelByteSDK.GetClientRegistry().GetApi().GetLobby();
Session ??= AccelByteSDK.GetClientRegistry().GetApi().GetSession();
...
} -
A static helper property to track the active session:
public static SessionV2GameSession CachedSession { get; protected set; }
The AccelByteWarsOnlineSessionModels
file contains several predefined helper components, including:
-
A string constant to store the session template name, based on the Session Template configured in the Admin Portal:
public const string NoneSessionTemplateName = "unity-elimination-none";
-
A helper dictionary that maps in-game modes to session request models based on the session template name:
public static readonly Dictionary<InGameMode, Dictionary<GameSessionServerType, SessionV2GameSessionCreateRequest>> SessionCreateRequestModels = new()
{
{
InGameMode.None, new()
{
{
GameSessionServerType.None,
new SessionV2GameSessionCreateRequest()
{
type = SessionConfigurationTemplateType.NONE,
joinability = SessionV2Joinability.OPEN,
configurationName = NoneSessionTemplateName,
matchPool = NoneSessionTemplateName,
}
}
}
}
...
}; -
A helper function to retrieve the session request model by in-game mode:
public static SessionV2GameSessionCreateRequest GetGameSessionRequestModel(
InGameMode gameMode,
GameSessionServerType serverType)
{
if (!SessionCreateRequestModels.TryGetValue(gameMode, out var matchTypeDict))
{
return null;
}
matchTypeDict.TryGetValue(serverType, out var request);
return request;
}
Create session
This section will guide you through sending a session creation request using the AGS SDK.
-
Open the
SessionEssentialsWrapper_Starter
class and create the following function. This function first leaves the active session (if one exists) before creating a new session. Once the request is complete, it invokes the assigned delegate and caches the session locally.public override void CreateGameSession(
SessionV2GameSessionCreateRequest request,
ResultCallback<SessionV2GameSession> onComplete)
{
// Leave the existing session before creating a new session.
if (CachedSession != null)
{
LeaveGameSession(CachedSession.id, (leaveResult) =>
{
// Abort only if there's an error and it's not due to a missing session.
if (leaveResult.IsError && leaveResult.Error.Code != ErrorCode.SessionIdNotFound)
{
BytewarsLogger.LogWarning($"Failed to create game session. Error {leaveResult.Error.Code}: {leaveResult.Error.Message}");
onComplete?.Invoke(Result<SessionV2GameSession>.CreateError(leaveResult.Error));
return;
}
CreateGameSession(request, onComplete);
});
return;
}
// Create a new session.
Session.CreateGameSession(request, (result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning($"Failed to create game session. Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Success to create game session. Session ID: {result.Value.id}");
}
CachedSession = result.Value;
onComplete?.Invoke(result);
});
}
Leave session
This section will guide you through sending a leave session request using the AGS SDK.
-
Open the
SessionEssentialsWrapper_Starter
class and create the following function. This function sends a request to leave the session using its ID. Once the request is complete, it invokes the assigned delegate.public override void LeaveGameSession(
string sessionId,
ResultCallback onComplete)
{
Session.LeaveGameSession(sessionId, (result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning(
$"Failed to leave game session with ID: {sessionId}. " +
$"Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Success to leave game session. Session ID: {sessionId}");
}
CachedSession = null;
onComplete?.Invoke(result);
});
}
Join session
This section will guide you through sending a join session request using the AGS SDK.
-
Open the
SessionEssentialsWrapper_Starter
class and create the following function. This function first leaves the active session (if one exists) before joining a new one. Once the request is complete, it invokes the assigned delegate and caches the session locally.public override void JoinGameSession(
string sessionId,
ResultCallback<SessionV2GameSession> onComplete)
{
// Leave the existing session before joining a new session.
if (CachedSession != null)
{
LeaveGameSession(CachedSession.id, (leaveResult) =>
{
// Abort only if there's an error and it's not due to a missing session.
if (leaveResult.IsError && leaveResult.Error.Code != ErrorCode.SessionIdNotFound)
{
BytewarsLogger.LogWarning($"Failed to join game session. Error {leaveResult.Error.Code}: {leaveResult.Error.Message}");
onComplete?.Invoke(Result<SessionV2GameSession>.CreateError(leaveResult.Error));
return;
}
JoinGameSession(sessionId, onComplete);
});
return;
}
// Join a new session.
Session.JoinGameSession(sessionId, (result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning($"Failed to join game session. Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Success to join game session. Session ID: {result.Value.id}");
}
CachedSession = result.Value;
onComplete?.Invoke(result);
});
}
Send session invitation
This section will guide you through sending a session invitation request to another player using the AGS SDK.
-
Open the
SessionEssentialsWrapper_Starter
class and create the following function. This function sends a session invitation to another player by providing the session ID and the invitee's user ID. Once the request is complete, it invokes the assigned delegate.public override void SendGameSessionInvite(
string sessionId,
string inviteeUserId,
ResultCallback onComplete)
{
Session.InviteUserToGameSession(
sessionId,
inviteeUserId,
(result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning(
$"Failed to send game session invite to {inviteeUserId}. " +
$"Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Success to send game session invite to {inviteeUserId}");
}
onComplete?.Invoke(result);
});
}
Reject session invitation
This section will guide you through sending a session invitation rejection request using the AGS SDK.
-
Open the
SessionEssentialsWrapper_Starter
class and create the following function. This function sends a request to reject a session invitation. Once the request is complete, it invokes the assigned delegate.public override void RejectGameSessionInvite(
string sessionId,
ResultCallback onComplete)
{
Session.RejectGameSessionInvitation(sessionId, (result) =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning(
$"Failed to reject game session with ID: {sessionId}. " +
$"Error {result.Error.Code}: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Success to reject game session. Session ID: {sessionId}");
}
onComplete?.Invoke(result);
});
}
Resources
-
The files used in this tutorial are available in the Unity Byte Wars GitHub repository.
- Assets/Resources/Modules/Play/SessionEssentials/Scripts/SessionEssentialsWrapper_Starter.cs
- Assets/Resources/Modules/Play/SessionEssentials/Scripts/SessionEssentialsModels.cs
- Assets/Resources/Modules/Play/OnlineSessionUtils/Scripts/AccelByteWarsOnlineSession.cs
- Assets/Resources/Modules/Play/OnlineSessionUtils/Scripts/AccelByteWarsOnlineSessionModels.cs