SDK でマッチセッションを作成する - 専用サーバーで参加可能なセッションを作成する - (Unity モジュール)
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 MatchSessionWrapper.cs
. This class will call the AGS Game SDK for Unity API to create a match session.
What's in the Starter Pack
The MatchSessionDSWrapper_Starter.cs
is a subclass of MatchSessionWrapper
, which has all functionalities from its parent. MatchSessionDSWrapper_Starter
class has several features related to session management. You can get more information from the MatchSessionWrapper.cs file on GitHub.
The Start()
function listens to specific events for the match session that includes create, join, and leave. The Lobby
event is also needed in this function to detect whenever there are changes to the members within the match session.
public void Start()
{
Lobby.SessionV2GameSessionMemberChanged += OnV2GameSessionMemberChanged;
GameManager.Instance.OnClientLeaveSession += LeaveGameSession;
LoginHandler.onLoginCompleted += OnLoginSuccess;
OnJoinCustomSessionCompleteEvent += OnJoinMatchSessionComplete;
OnCreateCustomMatchSessionCompleteEvent += OnCreateGameSessionResult;
OnLeaveCustomSessionCompleteEvent += OnLeaveGameSession;
}
Implement create match session
In this section, you will implement the AGS Game SDK for Unity API call to create a match session.
To create a match session, you need an instance of the
SessionV2GameSessionCreateRequest
class. It is already defined inMatchSessionConfig.cs
.Modify the
MatchSessionDSWrapper_Starter
to create function as shown below. The AGS Game SDK for Unity API call for creating game sessions is_session.CreateGameSession(request, OnCreateGameSessionResult);
.public void Create(InGameMode gameMode,
MatchSessionServerType sessionServerType,
Action<string> onCreatedMatchSession)
{
isCreateMatchSessionCancelled = false;
requestedSessionServerType = sessionServerType;
requestedGameMode = gameMode;
gameSessionV2 = null;
var config = MatchSessionConfig.MatchRequests;
if (!config.TryGetValue(gameMode, out var matchTypeDict))
{
return;
}
if (!matchTypeDict.TryGetValue(sessionServerType, out var request))
{
return;
}
BytewarsLogger.Log($"creating session {gameMode} {sessionServerType}");
MatchSessionDSWrapper_Starter.onCreatedMatchSession = onCreatedMatchSession;
ConnectionHandler.Initialization();
if (ConnectionHandler.IsUsingLocalDS())
{
request.serverName = ConnectionHandler.LocalServerName;
}
CreateCustomMatchSession(request);
}Modify the
OnCreateGameSessionResult
function inMatchSessionDSWrapper_Starter
. The function will handle the result of creating a game session. Here is how the completed function looks:private void OnCreateGameSessionResult(Result<SessionV2GameSession> result)
{
if (result.IsError)
{
BytewarsLogger.Log($"error: {result.Error.Message}");
onCreatedMatchSession?.Invoke(result.Error.Message);
}
else
{
BytewarsLogger.Log($"create session result: {result.Value.ToJsonString()}");
if (isCreateMatchSessionCancelled)
{
return;
}
gameSessionV2 = result.Value;
SessionCache
.SetJoinedSessionIdAndLeaderUserId(gameSessionV2.id, gameSessionV2.leaderId);
checkDSStatusCount = 0;
matchSessionWrapper.StartCoroutine(CheckSessionDetails());
}
}In the previous step, you handled the result callback for creating a game session. Since the current server type is a dedicated server, you also need to connect to the dedicated server when it is available. Modify
CheckSessionDetails
function inMatchSessionDSWrapper_Starter
according to this code:private IEnumerator CheckSessionDetails()
{
//... some code omitted for brevity
Session.GetGameSessionDetailsBySessionId(gameSessionV2.id, OnSessionDetailsCheckFinished);
}We use
Session.GetGameSessionDetailsBySessionID
and this function need a callback to handle the result, which will handle get session details and travel to the dedicated server.private void OnSessionDetailsCheckFinished(Result<SessionV2GameSession> result)
{
if (isCreateMatchSessionCancelled)
{
onCreatedMatchSession?.Invoke("Match session creation cancelled");
return;
}
if (result.IsError)
{
string errorMessage = result.Error.Message;
onCreatedMatchSession?.Invoke(errorMessage);
}
else
{
if (result.Value.dsInformation.status == SessionV2DsStatus.AVAILABLE)
{
onCreatedMatchSession?.Invoke("");
onCreatedMatchSession = null;
TravelToDS(result.Value, requestedGameMode);
}
else
{
if (checkDSStatusCount >= MaxCheckDSStatusCount)
{
onCreatedMatchSession?.Invoke("Failed to Connect to Dedicated Server in time");
}
else
{
matchSessionWrapper.StartCoroutine(CheckSessionDetails());
}
}
}
}