Skip to main content

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

Last updated on June 11, 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 go you through the process of connecting and registering your game server with AMS. Notice that you will implement the functions in reverse order from the AMS implementation flow.

  1. Open the MultiplayerDSAMSHelper_Starter.cs script.

  2. Create a function to connect to DS Hub and connect to its related events. As for the required dsId, you can get it from the amsWrapper getter function for the DedicatedServerId.

    public void HandleDSHubConnection()
    {
    string dsId = amsWrapper.DedicatedServerId;
    amsWrapper.ConnectToDSHub(dsId);
    amsWrapper.SubscribeDSHubEvents();
    }
  3. Create a new function to register the server by sending the ready message to AMS. Upon registering the server, call HandleDSHubConnection() to handle everything related to DSHub.

    private void RegisterDSToAMS()
    {
    BytewarsLogger.Log("[AMS] Sending ready to AMS");
    amsWrapper.SendReadyMessageToAMS();
    HandleDSHubConnection();
    }
  4. Create another function that will help us shut the server down when the on AMS connection drains.

    private void ShutdownDS()
    {
    BytewarsLogger.Log($"Shutting down DS..");
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.ExitPlaymode();
    #else
    Application.Quit();
    #endif
    }
  5. Create a new function to connect to AMS and bind the functions you made earlier with their respective events.

    private void HandleAMS()
    {
    amsWrapper.OnAMSConnectionOpened += RegisterDSToAMS;
    amsWrapper.OnAMSDrainReceived += ShutdownDS;
    amsWrapper.SubscribeAMSEvents();
    }
  6. Prepare a callback function to inform when the login process completes. If the server successfully logs in, call the HandleAMS() function to handle the AMS connection.

    private void OnLoginServerCompleted(Result result)
    {
    if (!result.IsError)
    {
    HandleAMS();
    }
    }
  7. Create a new function to call the amsWrapper login function and bind its ResultCallback to the function you created.

    private void LoginServer()
    {
    amsWrapper.LoginWithClientCredentials(OnLoginServerCompleted);
    }
  8. Call the LoginServer() function inside the Start() function. When the GameManager invokes OnDeregisterServer, call the ShutdownDS() to fully close the server.

    private void Start()
    {
    #if UNITY_SERVER
    ...

    GameManager.Instance.OnDeregisterServer += ShutdownDS;
    LoginServer();
    #endif
    }

Resources