Create match session with the SDK - Create joinable sessions with dedicated servers - (Unity module)
Unwrap the wrapper
In this tutorial, you will learn how to create a match session using the AccelByte Gaming Services (AGS) Game SDK for Unity. In the Byte Wars project, there is already an SDK wrapper created called MatchSessionDSWrapper_Starter.cs
. This class is a subclass of MatchSessionWrapper.cs
, which will call the AGS Game SDK for Unity API to create a match session.
- MatchSessionWrapper.cs: A C# script that contains most of the match session implementation.
- CS file:
Assets/Resources/Modules/MatchSession/Scripts/MatchSessionWrapper.cs
- CS file:
As mentioned above, MatchSessionDSWrapper_Starter.cs
is a subclass of the MatchSessionWrapper.cs
class that already implements several functions for creating and leaving match sessions.
MatchSessionWrapper.cs
has the CreateMatchSession
function that will create a match session by performing a set of operations, such as getting the game session configuration, binding the leave session event, and creating a game session by calling CreateSession
. You will use the CreateMatchSession
method from the MatchSessionWrapper.cs
class.
protected internal void CreateMatchSession(InGameMode inGameMode, GameSessionServerType sessionServerType)
{
GameManager.Instance.OnClientLeaveSession += LeaveCurrentGameSession;
GameManager.OnDisconnectedInMainMenu += DisconnectFromServer;
lobby.SessionV2GameSessionMemberChanged += OnV2GameSessionMemberChanged;
SelectedGameMode = inGameMode;
ConnectionHandler.Initialization();
Dictionary<InGameMode,
Dictionary<GameSessionServerType,
SessionV2GameSessionCreateRequest>> gameSessionConfig = GameSessionConfig.SessionCreateRequest;
if (!gameSessionConfig.TryGetValue(inGameMode, out var matchTypeDict))
{
BytewarsLogger.LogWarning($"GameSession Configuration not found");
return;
}
if (!matchTypeDict.TryGetValue(sessionServerType, out var request))
{
BytewarsLogger.LogWarning($"Match type not found");
return;
}
request.attributes = CreateMatchConfig.CreatedMatchSessionAttribute;
if (ConnectionHandler.IsUsingLocalDS())
{
request.serverName = ConnectionHandler.LocalServerName;
}
// Playing with Party additional code
if (!String.IsNullOrWhiteSpace(PartyHelper.CurrentPartyId))
{
string[] memberIds = PartyHelper.PartyMembersData.Select(data => data.UserId).ToArray();
request.teams = new SessionV2TeamData[]
{
new SessionV2TeamData()
{
userIds = memberIds
}
};
}
CreateSession(request);
}
What's in the Starter Pack
A C# Script to create match session using dedicated servers has been prepared for you. You will add additional code to it so it can connect to the AGS Game SDK for Unity.
- MatchSessionDSWrapper_Starter.cs: A C# script that contains most of the match session implementation.
- CS file:
Assets/Resources/Modules/MatchSessionDS/Scripts/MatchSessionDSWrapper_Starter.cs
- CS file:
Implement create match session
In this section, you will implement the AGS Game SDK for Unity API call to create a match session. This MatchSessionDSWrapper_Starter
class is a subclass of MatchSessionWrapper
.
Update
BindMatchSessionDSEvents
with the code below.OnCreateMatchSessionDS
is called fromMatchSessionWrapper
right after a match session is created.OnJoinMatchSessionDS
,OnCreateMatchCancelled
andOnLeaveSessionCompleted
are bound in this function.protected internal void BindMatchSessionDSEvents()
{
OnCreateMatchSessionDS += StartMatchSessionDS;
OnJoinMatchSessionDS += JoinMatchSessionDS;
OnCreateMatchCancelled += UnbindDSStatusChanged;
OnLeaveSessionCompleted += UnbindDSStatusChanged;
}Update the
UnbindMatchSessionDSEvents
function to remove all event listeners that were set up inBindMatchSessionDSEvents
.protected internal void UnbindMatchSessionDSEvents()
{
OnCreateMatchSessionDS -= StartMatchSessionDS;
OnJoinMatchSessionDS -= JoinMatchSessionDS;
OnCreateMatchCancelled -= UnbindDSStatusChanged;
OnLeaveSessionCompleted -= UnbindDSStatusChanged;
}In the
StartMatchSessionDS
function, you need to get the dedicated server (DS) status change notifications from the lobby. Update the function with the following code:private void StartMatchSessionDS()
{
lobby.SessionV2DsStatusChanged += OnDSStatusChanged;
}You need to handle the scenario where the player cancels creating a match session while the lobby notification event is still bound. You will address this in the
UnbindDSStatusChanged
function. Modify the function using this code:private void UnbindDSStatusChanged()
{
lobby.SessionV2DsStatusChanged -= OnDSStatusChanged;
}In the
JoinMatchSessionDS
function, you can expect the DS has already been spawned and there is a leader in that session, so you do not need to listen to lobby notifications. After receiving the API result, you need to check if the DS status is available and then callTravelToDS
. Implement the following code within the function:private void JoinMatchSessionDS(InGameMode gameMode, Result<SessionV2GameSession> result)
{
if (!result.IsError)
{
BytewarsLogger.Log($"Session Configuration Template Type : {result.Value.configuration.type}");
if (result.Value.dsInformation.status == SessionV2DsStatus.AVAILABLE)
{
TravelToDS(result.Value, gameMode);
}
}
else
{
BytewarsLogger.LogWarning($"Error: {result.Error.Message}");
}
}This function is a callback function that you attach to
lobby.SessionV2DsStatusChanged
. Whenever the DS status becomes available, this function calls theTravelToDS
function. Replace the existing function code with this:private void OnDSStatusChanged(Result<SessionV2DsStatusUpdatedNotification> result)
{
if (!result.IsError)
{
SessionV2DsStatus dsStatus = result.Value.session.dsInformation.status;
BytewarsLogger.Log($"DS Status: {dsStatus}");
if (dsStatus == SessionV2DsStatus.AVAILABLE)
{
lobby.SessionV2DsStatusChanged -= OnDSStatusChanged;
TravelToDS(result.Value.session, SelectedGameMode);
UnbindMatchSessionDSEvents();
}
}
else
{
BytewarsLogger.LogWarning($"Error: {result.Error.Message}");
}
}