Use the SDK to set up the server - Dedicated servers with AccelByte Multiplayer Servers (AMS) - (Unity module)
Understand the AMS dedicated server flow
Take some time to look at the flow of how a game server is managed by AccelByte Multiplayer Servers (AMS). For more information, read about the AMS dedicated server states in the AMS Integrate dedicated servers with SDK guide.
Unwrap the wrapper
You are now ready to use AMS functions provided by the AccelByte Gaming Services (AGS) Game SDK.
The Byte Wars project uses a wrapper class named MultiplayerDSAMSWrapper
that act as the wrapper to cache and handle AMS-related functionalities when using the AGS Game SDK.
The MultiplayerDSAMSWrapper
class uses the ServerApiClient
, ServerAMS
, and ServerDSHub
classes provided by the AccelByte Game SDK. They interact with AGS to provide the ability to register and unregister your server, receive notifications from DSHub and AMS, and handle multiplayer features.
What's in the Starter Pack
The class MultiplayerDSAMSWrapper_Starter
has been provided for you to act as a starter version of MultiplayerDSAMSWrapper
so you can follow the tutorial from scratch. You can find the file in the Resources section and it consists of the following file:
- MultiplayerDSAMSWrapper_Starter file:
/Assets/Resources/Modules/MultiplayerDSEssentials/Scripts/MultiplayerDSAMSWrapper_Starter.cs
It already contains some functionalities:
Namespaces declaration for using the
ServerAMS
andServerDSHub
classes in the AGS Game SDKusing AccelByte.Core;
using AccelByte.Models;
using AccelByte.Server;Some Action events to give AMS notifications regarding connection and status changes.
public event Action OnAMSConnectionOpened = delegate {};
public event Action OnAMSDrainReceived = delegate {};
Integrate AMS using the SDK
Open Byte Wars in Unity.
Open the predefined
MultiplayerDSAMSWrapper_Starter
C# script. Define some variables forDedicatedServer
,ServerAMS
, andServerDSHub
classes, then initialize those functions inside theStart()
function to be able to call them.private DedicatedServer ds;
private ServerAMS ams;
private ServerDSHub dsHub;
void Start()
{
#if UNITY_SERVER
ds = AccelByteServerPlugin.GetDedicatedServer();
ams = AccelByteServerPlugin.GetAMS();
dsHub = MultiRegistry.GetServerApiClient().GetDsHub();
#endif
}Prepare a getter function to get the dedicated server ID. This
DSId
will be important for connecting to DSHub later. Its value can be retrieved from the provided config values inAccelByteServerPlugin
.public string DedicatedServerId => AccelByteServerPlugin.Config.DsId;
Start to prepare the server login. Create a callback function to inform when the login process completes.
private void OnLoginWithClientCredentialsCompleted(Result result, ResultCallback customCallback = null)
{
if (!result.IsError)
{
BytewarsLogger.Log("Server login success.");
}
else
{
BytewarsLogger.Log($"Server login failed. Error code: {result.Error.Code}, message: {result.Error.Message}");
}
customCallback?.Invoke(result);
}Create a wrapper function to call the
DedicatedServer
login function. The game server will try to log in with client credentials provided inAccelByteServerSDKOAuthConfig.json
.public void LoginWithClientCredentials(ResultCallback resultCallback = null)
{
ds?.LoginWithClientCredentials(
result => OnLoginWithClientCredentialsCompleted(result, resultCallback)
);
}Create a callback function to invoke the
OnAMSConnectionOpened
event that will inform when the AMS service successfully connects and is ready to use.private void OnAMSConnected()
{
BytewarsLogger.Log("[AMS] AMS Connected!");
OnAMSConnectionOpened?.Invoke();
ams.OnOpen -= OnAMSConnected;
}Create a function to handle AMS notification events. Bind the previous
OnAMSConnectionOpened()
function to the AMSOnOpen
notification event and invoke theOnAMSDrainReceived
Action event when AMSOnDrainReceived
triggers.public void SubscribeAMSEvents()
{
if (ams == null)
{
return;
}
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX
OnAMSConnectionOpened?.Invoke();
#else
ams.OnOpen += OnAMSConnected;
#endif
ams.OnDrainReceived += () => OnAMSDrainReceived?.Invoke();
}Create another function to send a ready message to AMS, informing that our server is ready. By sending the ready message, you are actually registering the server to AMS.
public void SendReadyMessageToAMS()
{
ams?.SendReadyMessage();
}Now that your dedicated server is ready, the game session will need to claim it. Prepare a wrapper function to connect to DSHub and get updates for your dedicated server. It will require your
dsId
to connect.public void ConnectToDSHub(string serverId)
{
dsHub?.Connect(serverId);
}Prepare two callback functions that will handle on connected or on disconnected to DSHub. On disconnect, it will retry to reconnect to DSHub by calling the
ConnectToDSHub()
function.private void OnDSHubConnected()
{
BytewarsLogger.Log($"DS connected to DSHub");
}private void OnDSHubDisconnected(WsCloseCode wsCloseCode)
{
BytewarsLogger.Log("DS disconnected from DSHub, trying to reconnect..");
// Reconnect to DS
if (!String.IsNullOrEmpty(DedicatedServerId) && dsHub != null)
{
ConnectToDSHub(DedicatedServerId);
}
}Create a wrapper function to subscribe to DSHub events and bind the functions you created earlier to their respective events.
public void SubscribeDSHubEvents()
{
dsHub.OnConnected += OnDSHubConnected;
dsHub.OnDisconnected += OnDSHubDisconnected;
}
Resources
- GitHub Link to the files in the ByteWars repository: