Documentation
Ask or search…
K
Links

Get the List of Lobbies

Here we're going to get a list of existing lobbies and select a lobby to join
Start by creating a UI for listing lobbies. This UI should include components such as List Lobby, Lobby Item, UI In Lobby and In Room, and UI for when in Lobby but not in Room.
In MenuSceneManager.cs, define the necessary variables to manage this list of lobbies.
...
[Header("List Lobbies")] [SerializeField]
/* Transform of Gameobject that'll contain list lobby item */
[SerializeField] private Transform _listLobbiesContentTransform;
/* Lobby Item prefab */
[SerializeField] private LobbyItem _lobbyItemPrefab;
/* List store all lobby item created */
public List<LobbyItem> listLobbies = new();
...
In MenuSceneManager.cs, create a function to list lobbies
public async void ListLobbies()
{
try
{
QueryLobbiesOptions queryLobbiesOptions = new QueryLobbiesOptions
{
Count = 25,
Filters = new List<QueryFilter>
{
/* Just get the lobby's available slots using the filter. */
new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
},
Order = new List<QueryOrder>
{
new QueryOrder(false, QueryOrder.FieldOptions.Created)
}
};
QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(queryLobbiesOptions);
/* Disative all old lobby item in list */
foreach (Transform child in _listLobbiesContentTransform)
{
child.gameObject.SetActive(false);
}
listLobbies.Clear();
/* Show every lobby item in list */
int i = 0;
foreach (Lobby lobby in queryResponse.Results)
{
LobbyItem lobbyItem;
try
{
lobbyItem = _listLobbiesContentTransform.GetChild(i).GetComponent<LobbyItem>();
}
catch (Exception)
{
lobbyItem = Instantiate(_lobbyItemPrefab, _listLobbiesContentTransform);
}
lobbyItem.SetData("#" + (i + 1), lobby.Id, lobby.LobbyCode, lobby.Name);
lobbyItem.SetOnClickJoin(OnClickJoinLobby);
listLobbies.Add(lobbyItem);
}
}
catch (Exception e)
{
Debug.LogError("Exception : " + e.ToString());
}
}
Add onClick.AddListener for all your buttons, connecting them to their respective functions.
When you are signed in but not in any lobby, call GetListLobbies to fetch the list of lobbies. After a set interval, invoke a function to retrieve the list of lobbies, creating a real-time effect.
// ...
AuthenticationService.Instance.SignedIn += delegate
{
...
StopCoroutine(IEGetListLobbies()); // Stop old Coroutine if exist
StartCoroutine(IEGetListLobbies()); // Start new Coroutine
}
// ...
IEnumerator IEGetListLobbies(float delayTime = 3f)
{
while (AuthenticationService.Instance.IsSignedIn && string.IsNullOrEmpty(currentLobbyId))
{
yield return new WaitForSeconds(delayTime);
ListLobbies();
}
}