Skip to content

Getting Started

Installation

First, import Cobra Logging from the Unity Asset Store. This step is necessary before you can import the Cobra Servers package into your Unity project.

After importing the Cobra Servers package into Unity, locate and extract the zip file from the root folder to your desired location. Open the Visual Studio solution file, then build and run the ByteCobra.Servers.Server project to run the server locally without Docker.

To run the server in Docker, navigate to the extracted directory containing the Dockerfile and execute the following command:

docker-compose -p bytecobra build --no-cache && docker-compose -p bytecobra up -d

Please see the Docker section of the documentation for more information about Docker.

Usage

Drag the prefab from the prefabs folder into your Unity scene and set the base URL. If you are running the service locally you can use the default base URL.

Adding New Servers

The following example shows you how you can add a new server to the list in a Unity MonoBehaviour type class:

public ServerListBehaviour ServerList;

protected async void Start()
{
    string name = "My Server Name";
    string host = "127.0.0.1";
    ushort port = 7777;
    ushort maxPlayers = 50;
    ushort playersOnline = 0;

    // You can add additional data columns using key-value pairs
    Dictionary<string, string> customData = new Dictionary<string, string>()
    {
        {"Mod Name", "My Mod" },
        {"Mod Version", "10" },
        {"Game Mode", "Sandbox" }
    };

    // How often to ping the master server, if the game server stops pinging
    // then it will be removed from the server list.
    TimeSpan pingInterval = TimeSpan.FromSeconds(30);

    // Add the server
    await ServerList.Api.StartServerAsync(name, host, port, maxPlayers, pingInterval, playersOnline, customData);
}

Getting Servers

The following example shows you how you can get the server list from the service to show it in Unity:

public ServerListBehaviour ServerList;

protected async void Start()
{
    // Get all the servers
    var servers = await ServerList.Api.GetServersAsync();

    foreach (var server in servers)
    {
        string name = server.Name;
        string host = server.Host;
        ushort port = server.Port;
        ushort maxPlayers = server.MaxPlayers;
        ushort playersOnline = server.PlayersOnline;

        // Get custom data
        Dictionary<string, string> customData = server.GetCustomData();

        // Get a specific value from the custom data dictionary and convert it into an object
        int? modVersion = server.TryGetCustomData<int>("Mod Version");

        // Use this data to populate the UI...
    }
}

Rate Limits

The API features easily adjustable rate limits, ensuring fair usage and preventing abuse by controlling the number of requests per time period. To adjust the rate limits you can change the values of the attributes in the controller. Open the ByteCobra.Servers.Server project and modify the rate limit attributes in the controller for each endpoint (method). Adjust these values as necessary or keep the default settings.