LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 1.7 Preview
SDK 1.7 Preview
  • Welcome
  • Overview
    • Features
    • Roadmap
  • Getting started
    • Get the Unity SDK
    • Setup a project
      • 1. Scene setup
      • 2. Prefab setup
      • 3. Test your game locally
        • Local testing using builds
        • Local testing via Unity's Multiplayer Play Mode
        • Local testing via ParrelSync
      • 4. Test in the cloud
        • Deploy a Replication Server
        • Share builds
    • How to... ?
    • Single-player to multiplayer
    • Video tutorials
    • Samples and tutorials
      • Package samples
      • Sample Connection UIs
      • First Steps tutorial
        • 1. Basic syncing
          • 1.1 Animation parameters
          • 1.2 Sending commands
        • 2. Physics / Authority transfer
        • 3. Areas of interest
        • 4. Parenting entities
        • 5. Complex hierarchies
        • 6. Persistence
      • Campfire project
        • Game mechanics
        • Leveraging object pooling
        • Remote interactions: Chairs
        • Remote interactions: Trees
        • A unique object with complex state
        • Custom instantiation and destruction
        • Running a server-side NPC
        • Playing audio and particles
        • A simple text chat
      • Beginner's guide to networking
    • Troubleshooting
  • Manual
    • Unity Components
      • CoherenceSync
      • CoherenceBridge
      • CoherenceLiveQuery
      • CoherenceTagQuery
      • CoherenceGlobalQuery
      • CoherenceInput
      • CoherenceNode
      • PrefabSyncGroup
      • Order of execution
    • Networking state changes
      • Instantiate and Destroy Objects
      • Supported types
      • Messaging with Commands
      • Syncing child GameObjects
      • Animation
      • CoherenceSync references
      • [Sync] and [Command] Attributes
      • [OnValueSynced] Attribute
      • Creating your own syncable member
      • Custom Component Actions
      • Rigid Bodies
      • Interpolation
    • Authority
      • Authority transfer
      • Server-authoritative setup
    • Lifetime
      • Persistence
      • Uniqueness
      • Example: A global counter
    • Parenting network entities
      • Direct children CoherenceSyncs
      • Deeply-nested CoherenceSyncs
      • Nesting Prefabs at Edit time
    • Asset management
      • Instantiating from CoherenceSyncConfig
      • Instantiate via
      • Load via
    • Scene management
    • Multiple Connections within a Game Instance
    • Baking (code generation)
      • Conditional compilation
    • Replication Server
      • Rooms and Worlds
      • Replication Server API
    • Simulators (Servers)
      • Scripting: Client vs Simulator
      • Run local Simulators
      • World Simulators
      • Room Simulators
      • Advanced Simulator Authority
      • Simulator slugs
      • Build and Deploy
      • Command-line arguments
    • Client Connections
    • Optimization
      • Areas of Interest
      • Level of Detail (LOD)
      • Profiling
      • Simulation Frequency
    • Project Settings
    • Advanced topics
      • Big worlds
        • World Origin Shifting
        • Load balancing
      • Competitive games
        • Simulation Frame
        • Determinism, Prediction and Rollback
      • Team workflows
        • Version Control integration
        • Continuous Integration
      • Schema explained
        • Specification
        • Field settings
        • Archetypes
      • Code stripping
      • Replication Server CLI
      • Single-player gameplay
    • Scripting API
  • Hosting
    • Choosing where to host
    • coherence Cloud
      • Online Dashboard
      • Manage Worlds
      • Configure Rooms
      • Player Accounts
      • Game Services
        • Lobbies
        • Cloud Storage
        • Key-Value Store (Legacy)
      • APIs
        • Worlds
        • Rooms
        • Lobbies
        • Cloud Storage
        • Key-Value Store (Legacy)
    • Peer-to-peer
      • Implementing Client hosting
        • Steam Relay
        • Epic Online Services (EOS) Relay
        • Azure PlayFab Relay
  • Support
    • Release notes
    • Glossary
    • Unreal Engine support
    • WebGL support
    • ECS / DOTS support
    • Known issues
    • Upgrade guide
      • Upgrade 1.6 -> 1.7
      • Upgrade 1.5 -> 1.6
      • Upgrade 1.4 -> 1.5
      • Upgrade 1.3 -> 1.4
      • Upgrade 1.2 -> 1.3
      • Upgrade 1.1 -> 1.2
      • Upgrade 1.0 -> 1.1
      • Upgrade 0.10 -> 1.0
      • Upgrade 0.9 -> 0.10
    • Credit cost & pricing
    • Report a bug
Powered by GitBook
On this page
  • Requirements
  • Lobbies Service API
  • Lobby and Player Attributes
  • Matchmaking
  • Lobby Session
  • Game Session
  • Handling Lobbies when logging in

Was this helpful?

Export as PDF
  1. Hosting
  2. coherence Cloud
  3. APIs

Lobbies

Was this helpful?

Requirements

Since Lobbies use under the hood for the Game Session, you must have at least one region enabled in the Rooms section of your dashboard.

cannot currently use lobbies because a valid player account is required.

Lobbies Service API

Accessible from CoherenceBridge.CloudService.CloudRooms, the Lobbies Service API has access to every endpoint you need to interface with the Lobbies feature.

Lobby and Player Attributes

Attributes are one of the main avenues available for customizing the Lobbies logic and fitting it to the needs of your game project.

Every Player in the Lobby and the Lobby itself can have custom Attributes associated to them.

An Attribute is defined by:

  • Key: Identifier for the Attribute.

  • Value: Value of the Attribute, can be either an integer number or a string.

  • Index: A predetermined value that will allow the Attribute to be used in Filter Expressions.

  • Aggregate: This only applies to the lobby indexes and can be: “sum”, “avg”, “min”, “max” or “owner” for the integer indexes or “owner” for the string indexes. When a player joins/leaves the lobby their indexes are aggregated into the lobby indexes.

  • Public: Public Attributes will be returned and visible to all Players.

Player or Lobby Attributes cannot be deleted, you can only add new ones or update existing ones.

If you wish to stop using an Attribute, we recommend setting a default value that you can interpret as such, like an empty string for a string Attribute, or a zero value for an integer Attribute.

Indexes and Aggregators

Indexed Attributes are aggregated from the Player Attributes into a Lobby Attribute.

For example, let's say every Player in my Lobby has a Matchmaking integer rating value Attribute associated to him, and we index this Attribute to n1.

We want Players to be able to do Matchmaking with Lobbies using the average Matchmaking rating of all the players present in the Lobby. The Lobby will have a "Matchmaking rating average" integer Attribute associated with it, that is also indexed to n1, with an aggregator of type "avg" to do the average of every Player present in the Lobby.

Every time a Player joins or leaves the Lobby, the Lobby Attribute indexed to n1 will be updated accordingly.

When you're entering the Matchmaking phase, you can use a Lobby Filter Expression to search for specific Lobbies that match the current rating of the Player that is initiating the Matchmaking phase.

Matchmaking

In order to start the Matchmaking process, you can use the FindOrCreateLobby method. Based on the given parameters to find a Lobby, the response of this method will be either an existing Lobby or a fresh new Lobby where your player will be the owner.

The bread and butter of the Matchmaking logic can be done through our powerful filter expression building system. The FindOrCreateLobby method accepts up to three different filters.

Lobby Filter Expressions

This is similar to the WHERE clause in an SQL-like expression. You can build filter expressions using Lobby Attribute indexes and most of the Lobby properties.

The player can use up to three filter expressions to filter out Lobbies. The expressions are tried in order until one of them succeeds. This can be used to increasingly expand the search criteria and can be very efficient if the amount of players is low.

Currently, these are the properties that you can use to customize your Matchmaking process:

  • Room Regions

  • Tags

  • Max Players

  • Number of Players

  • Available Slots

  • Simulator Slug

  • Is Private Lobby

  • Integer Attribute Index

  • String Attribute Index

For example:

  • “region in [‘us’, ‘eu’] and (s1 = ‘map1’ or s1 = ‘map2’)”

In Unity, you will build this filter expression using the builder pattern, if we wanted to match using the expression from the example, we would do it like this:

async void Start()
{
    PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();
    var cloudService = playerAccount.Services;
    var regions = await cloudService.Rooms.RefreshRegionsAsync();

    // Build filter for regions and either map1 or map2
    var lobbyFilter = new LobbyFilter()
        .WithAnd()
        .WithRegion(FilterOperator.Any, new List<string>(regions))
        .WithOr()
        .WithStringAttribute(FilterOperator.Equals, StringAttributeIndex.s1, "map1")
        .WithStringAttribute(FilterOperator.Equals, StringAttributeIndex.s1, "map2")
        .End();

    // You can use the ToString method to debug what your filters look like
    // This outputs: “region in [‘us’, ‘eu’] and (s1 = ‘map1’ or s1 = ‘map2’)”
    Debug.Log($"{lobbyFilter.ToString()}");

    var findOptions = new FindLobbyOptions { Limit = 20, LobbyFilters = new() { lobbyFilter } };

    // If we don't find a suitable Lobby, we will create one with Lobby Attribute map1 and the first available region
    var mapAttribute = new CloudAttribute("map", "map1", StringAttributeIndex.s1, StringAggregator.None, true);

    var createOptions = new CreateLobbyOptions { LobbyAttributes = new() { mapAttribute }, Region = regions[0] };

    var lobbies = cloudService.Rooms.LobbyService;

    // We start the Matchmaking process with the coherence Cloud
    lobbySession = await lobbies.FindOrCreateLobbyAsync(findOptions, createOptions);
    var lobby = lobbySession.LobbyData;

    Debug.Log($"Lobby found or created: {lobby.Name}.");

    // All players in a lobby should join the room for a play session once it has been started.
    // This step can be skipped if a CoherenceBridge's 'Player Account' option has been set to 'Main User'.
    lobbies.OnPlaySessionStarted += OnLobbyPlaySessionStarted;

    // When ready (e.g. the lobby is full), the play session can be started using LobbyOwnerActions.StartGameSessionAsync:
    await lobbySession.LobbyOwnerActions.StartGameSessionAsync();
}

void OnLobbyPlaySessionStarted(string lobbyId, RoomData room)
{
    if (CoherenceBridgeStore.TryGetBridge(gameObject.scene, out var bridge))
    {
        Debug.Log($"Lobby play session has started. Joining its room {room.RoomName}.");
        bridge.JoinRoom(room);
    }
}

Lobby Session

When you successfully join a Lobby, an instance of type LobbySession will be returned via the Lobbies API.

You can use this LobbySession instance to interface with the Lobby itself, you will find methods to leave the Lobby, update your Player Attributes or send a message to other Players. You will also be able to query all the information about the Lobby itself, like every Player present, their Attributes, maximum Players and other Lobby properties.

Additionally, if you're the owner of the Lobby, you will have an instance of type LobbyOwnerActions available to you (for non-owners this instance will be null referenced). As an owner of the Lobby, you will be able to kick other Players, update the Attributes of the Lobby, or start the Game Session.

LobbySession instance will have a number of callbacks you can subscribe to that will inform you of any updates that happen to the Lobby, like a Player joining or leaving, or Attributes being updated.

If a Room associated with a Lobby is closed, Players will also receive a callback.

Game Session

When the owner of the Lobby starts the Game Session, every Player will receive a callback with the Room endpoint data that can be used to connect to the Replication Server using the associated CoherenceBridge instance.

If you need to perform custom logic before actually joining the Room, you can subscribe a custom listener through the LobbiesService instance in CoherenceBridge.CloudService.Rooms.LobbyService.OnPlaySessionStarted. If no custom listener is registered, coherence will automatically connect to the Replication Server.

Handling Lobbies when logging in

If the player has closed the game abruptly without leaving the Lobby, when the game is restarted and the Player authenticates with the coherence Cloud, in the Login response you will receive the ID of the Lobbies that the Player is currently a part of. You can use these Lobby IDs to fetch again the LobbySession instance from the Lobbies Service API.

Additionally, the Lobby might have a current Game Session active, so you can use the Room data that is available to reconnect to the Replication Server Room.

Here is an example of how to handle this scenario:

using Coherence.Cloud;
using Coherence.Toolkit;
using UnityEngine;

class LobbyLoginExample : MonoBehaviour
{
    async void Start()
    {
        // Log in to coherence CLoud using a guest player account.
        var loginOperation = await CoherenceCloud.LoginAsGuest();

        // We can acquire ids of the lobbies that the player is in
        // from the completed login operation
        var lobbyIds = loginOperation.LobbyIds;
        if (lobbyIds.Count == 0)
        {
            return;
        }

        var lobbyId = lobbyIds[0];
        var lobbies = loginOperation.Result.Services.Rooms.LobbyService;

        // We fetch a LobbySession instance from the Lobby Service API
        var lobbySession = await lobbies.GetActiveLobbySessionForLobbyId(lobbyId);
        if (lobbySession is null)
        {
            return;
        }

        // We can determine if a play session is active by checking if the LobbyData contains any RoomData.
        if (lobbySession.LobbyData.RoomData is { } room && CoherenceBridgeStore.TryGetBridge(gameObject.scene, out var bridge))
        {
            // The Lobby has an ongoing Game Session, so we reconnect to the Room using the CoherenceBridge
            bridge.JoinRoom(room);
        }
        else
        {
            // The Lobby doesn't have an ongoing Game Session, here we can update our UI accordingly
        }
    }
}

Rooms
Simulators