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;
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("Current menu is not server type selection menu while try to create match with DS");
return;
}
matchSessionDSWrapper.CreateMatchSession(selectedGameMode, selectedSessionServerType);
ShowLoading(
"Creating Match Session (Dedicated Server)...",
"Creating Match Session (Dedicated Server) is timed out.",
createMatchSessionTimeoutSec,
CancelCreateMatch);
}Go to
InitWrapper
and implement the following code within the function:private void InitWrapper()
{
if (matchSessionDSWrapper == null)
{
matchSessionDSWrapper = 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.BindEvents();
matchSessionDSWrapper.BindMatchSessionDSEvents();
matchSessionDSWrapper.OnCreatedMatchSession += OnCreatedMatchSession;
matchSessionDSWrapper.OnJoinedMatchSession += OnJoinedMatchSession;
matchSessionDSWrapper.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
}private void UnbindMatchSessionEvent()
{
matchSessionDSWrapper.UnbindEvents();
matchSessionDSWrapper.UnbindMatchSessionDSEvents();
matchSessionDSWrapper.OnCreatedMatchSession -= OnCreatedMatchSession;
matchSessionDSWrapper.OnJoinedMatchSession -= OnJoinedMatchSession;
matchSessionDSWrapper.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()
{
Reset();
ShowInfo("Match session creation cancelled");
matchSessionDSWrapper.OnCreateMatchCancelled?.Invoke();
matchSessionDSWrapper.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.