Put it all together - Create joinable sessions with dedicated servers - (Unity module)
Hook up the create, leave, and display session UI
In this tutorial, you will connect the UI to create a match session by using the MatchSessionDSWrapper_Starter
class, which already incorporates the AGS Game SDK implementation. The CreateMatchSessionDSHandler_Starter
class has been provided as a UI handler for this tutorial.
Open the
CreateMatchSessionDSHandler_Starter.cs
and add the code below:private MatchSessionDSWrapper_Starter matchSessionDSWrapper_starter;
You will use
matchSessionDSWrapper.CreateMatchSession
to create a match session. Go to theClickDedicatedServerButton
and replace the code with this:public void ClickDedicatedServerButton()
{
InGameMode selectedGameMode = InGameMode.None;
GameSessionServerType selectedSessionServerType =
TutorialModuleManager.Instance.IsModuleActive(TutorialType.MultiplayerDSEssentials) ?
GameSessionServerType.DedicatedServerAMS :
GameSessionServerType.DedicatedServer;
MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
if (menu is MatchSessionServerTypeSelection serverTypeSelection)
{
selectedGameMode = serverTypeSelection.SelectedGameMode;
InitWrapper();
}
else
{
BytewarsLogger.LogWarning("The current server type selection menu is the wrong menu to try to create matches with dedicated servers.");
return;
}
matchSessionDSWrapper_starter.CreateMatchSession(selectedGameMode, selectedSessionServerType);
ShowLoading(
"Creating Match Session (Dedicated Server)...",
"Creating Match Session (Dedicated Server) has timed out.",
createMatchSessionTimeoutSec,
CancelCreateMatch);
}Go to
InitWrapper
and implement the following code within the function:private void InitWrapper()
{
if (matchSessionDSWrapper_starter == null)
{
matchSessionDSWrapper_starter = TutorialModuleManager.Instance.GetModuleClass<MatchSessionDSWrapper_Starter>();
}
BindMatchSessionEvent();
MatchSessionServerTypeSelection.OnBackButtonCalled -= OnBackButtonFromServerSelection;
MatchSessionServerTypeSelection.OnBackButtonCalled += OnBackButtonFromServerSelection;
}Now, set up the event binding in the
BindMatchSessionEvent
function along with the unbinding inUnbindMatchSessionEvent
.private void BindMatchSessionEvent()
{
matchSessionDSWrapper_starter.BindEvents();
matchSessionDSWrapper_starter.BindMatchSessionDSEvents();
matchSessionDSWrapper_starter.OnCreatedMatchSession += OnCreatedMatchSession;
matchSessionDSWrapper_starter.OnJoinedMatchSession += OnJoinedMatchSession;
matchSessionDSWrapper_starter.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
}private void UnbindMatchSessionEvent()
{
matchSessionDSWrapper_starter.UnbindEvents();
matchSessionDSWrapper_starter.UnbindMatchSessionDSEvents();
matchSessionDSWrapper_starter.OnCreatedMatchSession -= OnCreatedMatchSession;
matchSessionDSWrapper_starter.OnJoinedMatchSession -= OnJoinedMatchSession;
matchSessionDSWrapper_starter.OnLeaveSessionCompleted -= UnbindMatchSessionEvent;
}OnCreatedMatchSession
is responsible for updating the loading status and showing the cancel button. This button usesCancelCreateMatch
as a callback. To be able to cancel matchmaking, update theCancelCreateMatch
function with the code below.private void CancelCreateMatch()
{
ShowInfo("Match session creation canceled");
matchSessionDSWrapper_starter.OnCreateMatchCancelled?.Invoke();
matchSessionDSWrapper_starter.CancelCreateMatchSession();
}Play the game from the Unity editor. If successful, you will see this log:
[MatchSessionWrapper.cs] [OnCreateSessionCompleted] [Log] [linenumber] - Successfully created the match session
Congratulations! You successfully connected the UI with the wrapper class to create a match session.
Resources
- The file used in this tutorial is available in the Unity Byte Wars GitHub repository.