Put it all together - Introduction to Session - (Unity module)
Connect the UI to create, leave, and display current session status
-
Open the
CreateSessionMenu_Starter
class and define a variable to store the wrapper you created.private SessionEssentialsWrapper_Starter sessionEssentialsWrapper;
-
Replace the
CreateSession()
function with the code below. This function sends a request to create a new session. The session request details such as the session template name, joinability, and type are retrieved from theGetGameSessionRequestModel()
, a helper function from theAccelByteWarsOnlineSessionModels
class.private void CreateSession()
{
SessionV2GameSessionCreateRequest request =
AccelByteWarsOnlineSessionModels.GetGameSessionRequestModel(InGameMode.None, GameSessionServerType.None);
if (request == null)
{
widgetSwitcher.ErrorMessage = InvalidSessionTypeMessage;
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
widgetSwitcher.LoadingMessage = CreatingSessionMessage;
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
sessionEssentialsWrapper.CreateGameSession(request, OnCreateSessionComplete);
} -
Create a new callback function to handle when the session is created. This function displays the created session ID and switches the menu to the success state.
private void OnCreateSessionComplete(Result<SessionV2GameSession> result)
{
if (result.IsError)
{
widgetSwitcher.ErrorMessage = result.Error.Message;
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
sessionIdText.text = result.Value.id;
createSessionPanel.gameObject.SetActive(false);
sessionResultPanel.gameObject.SetActive(true);
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
} -
Replace the
LeaveSession()
function with the code below. This function sends a request to leave the current active session.private void LeaveSession()
{
widgetSwitcher.LoadingMessage = LeavingSessionMessage;
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
sessionEssentialsWrapper.LeaveGameSession(sessionIdText.text, OnLeaveSessionComplete);
} -
Create the function below to handle when the leave session request is complete. This function switches the menu back to the default state to display the create session button.
private void OnLeaveSessionComplete(Result result)
{
if (result.IsError)
{
widgetSwitcher.ErrorMessage = result.Error.Message;
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
createSessionPanel.gameObject.SetActive(true);
sessionResultPanel.gameObject.SetActive(false);
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
} -
Finally, replace the
OnEnable()
function with the code below. It initializes the wrapper and displays the last session if any.private void OnEnable()
{
// Reset to default state.
createSessionPanel.gameObject.SetActive(true);
sessionResultPanel.gameObject.SetActive(false);
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
sessionEssentialsWrapper ??= TutorialModuleManager.Instance.GetModuleClass<SessionEssentialsWrapper_Starter>();
if (!sessionEssentialsWrapper)
{
return;
}
// Show the last session result if any.
if (AccelByteWarsOnlineSession.CachedSession != null)
{
OnCreateSessionComplete(Result<SessionV2GameSession>.CreateOk(AccelByteWarsOnlineSession.CachedSession));
}
}
Resources
-
The files used in this tutorial are available in the Unity Byte Wars GitHub repository.