Skip to main content

Put it all together - Dedicated servers with AccelByte Multiplayer Servers (AMS) - (Unity module)

Last updated on October 24, 2024

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.

  1. Open the MultiplayerDSAMSHelper_Starter C# script.

  2. 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();
    }
  3. Next, create a function to deregister and disconnect the server from AMS.

    private void DeregisterDSFromAMS() 
    {
    amsWrapper.DisconnectFromAMS();
    }
  4. 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
    }
  5. 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();
    }
    }
  6. 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);
    }
  7. Finally, call the LoginServer() function inside the Start() function. Also, when the GameManager invokes the OnDeregisterServer event, call the DeregisterDSFromAMS() function to deregister the server from AMS and shut down the server.

    private void Start()
    {
    ...
    GameManager.Instance.OnDeregisterServer += DeregisterDSFromAMS;
    LoginServer();
    }

Resources