Put it all together - Dedicated servers with AccelByte Multiplayer Servers (AMS) - (Unity module)
What's on the menu
You will reuse the Play Online Menu from Introduction to Session, so you won't change any implementation related to the user interface (UI). Instead, you are going to add additional processes to make sure you are using the server with AccelByte Multiplayer Servers (AMS).
To add AMS implementation without changing the Multiplayer Menu scripts, a script has been prepared for you to modify called MultiplayerDSAMSHelper_Starter
that subscribes to events related to changes in the Multiplayer Menus.
- MultiplayerDSAMSHelper_Starter file:
/Assets/Resources/Modules/MultiplayerDSEssentials/Scripts/MultiplayerDSAMSHelper_Starter.cs
Here are some functionalities the file provides:
Reference to
MultiplayerDSAMSWrapper_Starter
that has been initialized.private MultiplayerDSAMSWrapper_Starter amsWrapper;
private void Start()
{
#if UNITY_SERVER
amsWrapper = TutorialModuleManager.Instance.GetModuleClass<MultiplayerDSAMSWrapper_Starter>();
#endif
}An event subscription from
GameManager
that will inform when the game server de-registers.GameManager.Instance.OnDeregisterServer += delegate {};
Connect server implementation With AMS
In this section, you will connect your functions created in the MultiplayerDSAMSWrapper_Starter
class with the MultiplayerDSAMSHelper_Starter
class to perform server logins, register the server, and deregister the server.
Open the
MultiplayerDSAMSHelper_Starter
C# script.Create a function to register the server by sending the ready message to AMS, connecting it to DS Hub, and subscribing to DS Hub events.
private void RegisterDSToAMS()
{
amsWrapper.SendReadyMessageToAMS();
string dsId = amsWrapper.DedicatedServerId;
amsWrapper.ConnectToDSHub(dsId);
amsWrapper.SubscribeDSHubEvents();
}Next, create a function to deregister and disconnect the server from AMS.
private void DeregisterDSFromAMS()
{
amsWrapper.DisconnectFromAMS();
}Create a function to shut down the server.
private void ShutdownDS()
{
BytewarsLogger.Log($"Shutting down DS..");
#if UNITY_EDITOR
UnityEditor.EditorApplication.ExitPlaymode();
#else
Application.Quit();
#endif
}Prepare a callback function to handle when the server login is completed. If the server logs in successfully, subscribe to the AMS events to handle the server for registration, de-registration, and shutdown.
private void OnLoginServerCompleted(Result result)
{
if (!result.IsError)
{
amsWrapper.OnAMSConnectionOpened += RegisterDSToAMS;
amsWrapper.OnAMSDrainSignalReceived += DeregisterDSFromAMS;
amsWrapper.OnAMSConnectionClosed += ShutdownDS;
amsWrapper.SubscribeAMSEvents();
}
}Next, create a new function to perform server logins. Use the previous function you created to handle the callback.
private void LoginServer()
{
amsWrapper.LoginWithClientCredentials(OnLoginServerCompleted);
}Finally, call the
LoginServer()
function inside theStart()
function. Also, when theGameManager
invokes theOnDeregisterServer
event, call theDeregisterDSFromAMS()
function to deregister the server from AMS and shut down the server.private void Start()
{
...
GameManager.Instance.OnDeregisterServer += DeregisterDSFromAMS;
LoginServer();
}
Resources
- GitHub Link to the files in the Unity Byte Wars repository: