Creating a lobby

When a player creates a new lobby, they can set the following properties with the create call:

  • Lobby name

  • Lobby visibility (public or private)

  • Lobby size (the maximum occupancy)

    • This is referred to as Max Players in the code.

  • Initial custom lobby data

  • This can include any arbitrary data. For example, map IDs and game modes.

  • Initial (host) player data

  • For example, player display names, skills and characters.

Note: When a player creates a public lobby, they automatically become the host.

When you create a lobby, you can include initial custom lobby data and custom player data (for the player creating the lobby). This can be more efficient as compared to creating a lobby with no data and then making separate UpdatePlayer and UpdateLobby calls.

Public lobbies

Public lobbies do not require a lobby code to join and are displayed in query results for anyone to join. The public properties for each lobby are visible to anyone.

The following code sample shows how to create a public lobby:

Copy
C#
try
{
    string lobbyName = "test lobby";
    int maxPlayers = 4;
    CreateLobbyOptions options = new CreateLobbyOptions();
    options.IsPrivate = false;

    // This is the "lobby scoped" data
    options.Data = new Dictionary<string, DataObject>()
    {
        {
            "ExampleIndexedPublicLobbyData", new DataObject(
                visibility: DataObject.VisibilityOptions.Public,
                value: "ExampleIndexedPublicLobbyData",
                index: DataObject.IndexOptions.S1)
        },
    };

    //Ensure you sign-in before calling Authentication Instance
    //See IAuthenticationService interface
    options.Player = new Player(
        id: AuthenticationService.Instance.PlayerId,
        // This is the "player scoped" data for the host player
        data: new Dictionary<string, PlayerDataObject>()
        {
            {
                "ExampleMemberPlayerData", new PlayerDataObject(
                    visibility: PlayerDataObject.VisibilityOptions.Member,
                    value: "ExampleMemberPlayerData")
            }
        });

    Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);

    //...
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

Private lobbies

Private lobbies require a lobby code to join and do not show up in query results.

The following code sample shows how to create a private lobby:

Copy
C#
try
{
    string lobbyName = "test lobby";
    int maxPlayers = 4;
    CreateLobbyOptions options = new CreateLobbyOptions();
    options.IsPrivate = true;

    // This is the "lobby scoped" data
    options.Data = new Dictionary<string, DataObject>()
    {
        {
            "ExampleIndexedPublicLobbyData", new DataObject(
                visibility: DataObject.VisibilityOptions.Public,
                value: "ExampleIndexedPublicLobbyData",
                index: DataObject.IndexOptions.S1)
        },
    };

    //Ensure you sign-in before calling Authentication Instance
    //See IAuthenticationService interface
    options.Player = new Player(
        id: AuthenticationService.Instance.PlayerId,
        // This is the "player scoped" data for the host player
        data: new Dictionary<string, PlayerDataObject>()
        {
            {
                "ExampleMemberPlayerData", new PlayerDataObject(
                    visibility: PlayerDataObject.VisibilityOptions.Member,
                    value: "ExampleMemberPlayerData")
            }
        });

    Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);

    //...
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

Active and inactive lobbies

Lobbies are marked as inactive if they haven’t been updated or sent a heartbeat request in the past 30 seconds. Inactive public lobbies do not appear in query results, and both public and private inactive lobbies are automatically deleted. Inactive lobbies can be reactivated by being updated or sending a heartbeat request.

Because there is a rate limit of 5 heartbeat calls per 30 seconds, users are capped at hosting 5 lobbies at a time. See Heartbeating a lobby for more information.