Put it all together - Create joinable sessions with peer-to-peer - (Unity module)
Connect the UI to the create, leave, and display current session status
In this tutorial, you will connect the UI to create a match session by using the MatchSessionP2PWrapper_Starter
class, which already incorporates the AGS Game SDK implementation. The CreateMatchSessionP2PHandler_Starter
class has been prepared for you as a UI handler for this tutorial.
Open the
CreateMatchSessionP2PHandler_Starter.cs
and add the code below:private MatchSessionP2PWrapper_Starter matchSessionP2PWrapper_starter;
Go to the
ClickPeerToPeerButton
and incorporate this code into the function:public void ClickPeerToPeerButton()
{
InGameMode selectedGameMode = InGameMode.None;
MenuCanvas menu = MenuManager.Instance.GetCurrentMenu();
if (menu is MatchSessionServerTypeSelection serverTypeSelection)
{
selectedGameMode = serverTypeSelection.SelectedGameMode;
InitWrapper();
}
else
{
BytewarsLogger.LogWarning("The current server type selection menu is not the correct menu while trying to create a match with a dedicated server.");
return;
}
matchSessionP2PWrapper_starter.CreateMatchSession(selectedGameMode, GameSessionServerType.PeerToPeer);
ShowLoading(
"Creating Match Session (P2P)...",
"Creating Match Session (P2P) has timed out.",
createMatchSessionTimeoutSec,
CancelCreateMatch);
}Go to
InitWrapper
and implement the following code:private void InitWrapper()
{
if (matchSessionP2PWrapper_starter == null)
{
matchSessionP2PWrapper_starter = TutorialModuleManager.Instance.GetModuleClass<MatchSessionP2PWrapper_Starter>();
}
...
}Now, set up the event binding in the
BindMatchSessionEvent
function along with the unbind inUnbindMatchSessionEvent
:private void BindMatchSessionEvent()
{
matchSessionP2PWrapper_starter.BindEvents();
matchSessionP2PWrapper_starter.BindMatchSessionP2PEvents();
matchSessionP2PWrapper_starter.OnCreatedMatchSession += OnCreatedMatchSession;
matchSessionP2PWrapper_starter.OnJoinedMatchSession += OnJoinedMatchSession;
matchSessionP2PWrapper_starter.OnLeaveSessionCompleted += UnbindMatchSessionEvent;
}private void UnbindMatchSessionEvent()
{
matchSessionP2PWrapper_starter.UnbindEvents();
matchSessionP2PWrapper_starter.UnbindMatchSessionP2PEvents();
matchSessionP2PWrapper_starter.OnCreatedMatchSession -= OnCreatedMatchSession;
matchSessionP2PWrapper_starter.OnJoinedMatchSession -= OnJoinedMatchSession;
matchSessionP2PWrapper_starter.OnLeaveSessionCompleted -= UnbindMatchSessionEvent;
}You have the
OnCreatedMatchSession
function that will be 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");
matchSessionP2PWrapper_starter.OnCreateMatchCancelled?.Invoke();
matchSessionP2PWrapper_starter.CancelCreateMatchSession();
}Play the game in the 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.