Lobbies - Code
[SOCL-Lobbies-03]
Experimental API
Please note that this feature currently includes an experimental API. Game makers will have to replace any experimental namespace when the finalized version is released.
Examples
These examples cover common use cases for Lobbies. The _beamContext variable used below is an instance of the BeamContext class and may be obtained by BeamContext.Default:
Create Lobby
The Create method will create a lobby with specified parameters which can be accessed through _beamContext.Lobby.Value.
The Create method requires:
namethe name of the lobby.LobbyRestrictionhas 2 possible values:Openmeans the lobby can be joined by any player who knows thelobbyId.Closedallows players to join the lobby by passcode.
gameTypeIdthe id of theSimGameTypewhich is a Beamable ContentObject for multiplayer matchmaking.
The Create method has a number of optional parameters:
descriptionadditional information about the lobby.playerTagslist of arbitrary name/value pairs associated with aLobbyPlayerfor custom player metadata or properties.maxPlayersdetermines maximum allowed number of players in the lobby. If not specified, it defaults to the maximum players set under teams inSimGameType.
passcodeLengththe length of the passcode that would be auto-generated by the backend.statsToIncludelist of stat keys to include with lobby requests.
public async Task CreateLobbyAsync(CreateLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Create(lobbyRecord.Name, lobbyRecord.Restriction, lobbyRecord.GameTypeId,
lobbyRecord.Description, lobbyRecord.PlayerTags, lobbyRecord.MaxPlayers, lobbyRecord.PasscodeLength);
Update Lobby
The Update method will update details about an existing lobby which can be accessed through _beamContext.Lobby.Value.
The Update method requires:
lobbyIda unique identifier for the existing lobby.LobbyRestrictionnewHosttheplayerIdof aLobbyPlayer.
The Update method has a number of optional parameters:
descriptiongameTypemaxPlayers
public async Task UpdateLobbyAsync(UpdateLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Update(ActiveLobby.lobbyId, lobbyRecord.Restriction, ActiveLobby.host,
lobbyRecord.Name, lobbyRecord.Description, lobbyRecord.GameTypeId, lobbyRecord.MaxPlayers);
Finding Lobbies
The FindLobbies method will find all open lobbies and will return a LobbyQueryResponse.
public async Task FindLobbiesAsync() => Lobbies = (await _beamContext.Lobby.FindLobbies()).results;
Finding Lobbies By Game Type
The FindLobbiesOfType method will find all lobbies of a particular game type and will return a LobbyQueryResponse.
The FindLobbiesOfType method has optional parameters for pagination:
limitdefaults to 100 if not specified.skipdefaults to 0 if not specified.
public async Task FindLobbiesOfTypeAsync(string gameType) =>
Lobbies = (await _beamContext.Lobby.FindLobbiesOfType(gameType)).results;
Join Lobby
The Join method will allow players to join an existing open lobby by lobbyId. Optionally, you can send playerTags with the join lobby request.
public async Task JoinLobbyAsync(JoinLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Join(lobbyRecord.LobbyId, lobbyRecord.PlayerTags);
Join Lobby By Passcode
The JoinByPasscode method will allow players to join an existing closed lobby by passcode. Optionally, you can send playerTags with the join lobby by passcode request.
public async Task JoinLobbyByPasscodeAsync(JoinLobbyByPasscodeRecord lobbyRecord) =>
await _beamContext.Lobby.JoinByPasscode(lobbyRecord.Passcode, lobbyRecord.PlayerTags);
Kick Player Out Of Lobby
The KickPlayer method will allow a host to remove a player with the given playerId from the lobby.
public async Task KickPlayerOutOfLobbyAsync(string playerId) => await _beamContext.Lobby.KickPlayer(playerId);
Leave Lobby
The Leave method will allow a player to leave the lobby and would notify the lobby that the player intends to leave.
public async Task LeaveLobbyAsync() => await _beamContext.Lobby.Leave();
Lobby Event
The following event is available
public Action<Lobby> OnDataUpdated;
Sample Code
using System.Collections.Generic;
using System.Threading.Tasks;
using Beamable;
using Beamable.Experimental.Api.Lobbies;
using UnityEngine;
public class BeamManager : MonoBehaviour
{
private BeamContext _beamContext;
public List<Lobby> Lobbies { get; private set; }
public Lobby ActiveLobby { get; private set; }
public string PlayerId { get; private set; }
private void Start() => SetupBeamable();
private async void SetupBeamable()
{
_beamContext = BeamContext.Default;
await _beamContext.OnReady;
_beamContext.Lobby.OnDataUpdated += OnLobbyDataUpdated;
PlayerId = _beamContext.PlayerId.ToString();
}
private void OnDestroy()
{
if (_beamContext == null) return;
_beamContext.Lobby.OnDataUpdated -= OnLobbyDataUpdated;
}
public async Task FindLobbiesAsync() => Lobbies = (await _beamContext.Lobby.FindLobbies()).results;
public async Task FindLobbiesOfTypeAsync(string gameType) =>
Lobbies = (await _beamContext.Lobby.FindLobbiesOfType(gameType)).results;
public async Task CreateLobbyAsync(CreateLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Create(lobbyRecord.Name, lobbyRecord.Restriction, lobbyRecord.GameTypeId,
lobbyRecord.Description, lobbyRecord.PlayerTags, lobbyRecord.MaxPlayers, lobbyRecord.PasscodeLength);
public async Task UpdateLobbyAsync(UpdateLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Update(ActiveLobby.lobbyId, lobbyRecord.Restriction, ActiveLobby.host,
lobbyRecord.Name, lobbyRecord.Description, lobbyRecord.GameTypeId, lobbyRecord.MaxPlayers);
public async Task JoinLobbyAsync(JoinLobbyRecord lobbyRecord) =>
await _beamContext.Lobby.Join(lobbyRecord.LobbyId, lobbyRecord.PlayerTags);
public async Task JoinLobbyByPasscodeAsync(JoinLobbyByPasscodeRecord lobbyRecord) =>
await _beamContext.Lobby.JoinByPasscode(lobbyRecord.Passcode, lobbyRecord.PlayerTags);
public async Task KickPlayerOutOfLobbyAsync(string playerId) => await _beamContext.Lobby.KickPlayer(playerId);
public async Task LeaveLobbyAsync() => await _beamContext.Lobby.Leave();
private void OnLobbyDataUpdated(Lobby lobby)
{
if (lobby == null) return;
ActiveLobby = lobby;
}
}
public record CreateLobbyRecord
{
public string Name { get; set; }
public LobbyRestriction Restriction { get; set; }
public string GameTypeId { get; set; }
public string Description { get; set; }
public List<Tag> PlayerTags { get; set; }
public int? MaxPlayers { get; set; }
public int? PasscodeLength { get; set; }
};
public record UpdateLobbyRecord
{
public LobbyRestriction Restriction { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string GameTypeId { get; set; }
public int? MaxPlayers { get; set; }
}
public record JoinLobbyRecord
{
public string LobbyId { get; set; }
public List<Tag> PlayerTags { get; set; }
}
public record JoinLobbyByPasscodeRecord
{
public string Passcode { get; set; }
public List<Tag> PlayerTags { get; set; }
}
Updated about 2 years ago
