Filter language based on player's preferred language

Hello!
I am creating a lobby for my first multiplayer game. However I can’t figure out that how I can filter the lobbies based on the player’s preference. Here is a code snippet of my lobby script:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
using UnityEngine;

public class Lobby : MonoBehaviour {

    public enum Language
    {
        English,
        Chinese,
        Hindi,
        Spanish,
        French,
        German,
        Portuguese,
        Russian,
        Japanese,
        Korean
    }
    public async void CreateLobby(string lobbyName,bool isPrivate, Language language) {
     
        CreateLobbyOptions options = new CreateLobbyOptions {
          
            IsPrivate = isPrivate,
            Data = new Dictionary<string, DataObject> {
                { KEY_START_GAME, new DataObject(DataObject.VisibilityOptions.Member,"0") },
                { LANGUAGE, new DataObject(DataObject.VisibilityOptions.Public, language.ToString()) }

            }
        };

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

        joinedLobby = lobby;

        OnJoinedLobby?.Invoke(this, new LobbyEventArgs { lobby = lobby });

    }

    public async void RefreshLobbyList() {
        try {
           
        } catch (LobbyServiceException e) {
            Debug.Log(e);
        }
    }

This is a code snippet of script which allows the player to create a lobby:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CreateLobby : MonoBehaviour {

    public static LobbyCreateUI Instance { get; private set; }


    [SerializeField] private Button languageButton;
    [SerializeField] private TMPro.TextMeshProUGUI languageDisplayText;
    private LobbyManager.Language language;


    private void Awake() {
        Instance = this;

        languageButton.onClick.AddListener(() => {
            switch (language)
            {
                default:
                case LobbyManager.Language.English:
                    language = LobbyManager.Language.Chinese;
                    break;
                case LobbyManager.Language.Chinese:
                    language = LobbyManager.Language.Hindi;
                    break;
                case LobbyManager.Language.Hindi:
                    language = LobbyManager.Language.Spanish;
                    break;
                case LobbyManager.Language.Spanish:
                    language = LobbyManager.Language.French;
                    break;
                case LobbyManager.Language.French:
                    language = LobbyManager.Language.German;
                    break;
                case LobbyManager.Language.German:
                    language = LobbyManager.Language.Portuguese;
                    break;
                case LobbyManager.Language.Portuguese:
                    language = LobbyManager.Language.Russian;
                    break;
                case LobbyManager.Language.Russian:
                    language = LobbyManager.Language.Japanese;
                    break;
                case LobbyManager.Language.Japanese:
                    language = LobbyManager.Language.Korean;
                    break;
                case LobbyManager.Language.Korean:
                    language = LobbyManager.Language.English;
                    break;
            }
            UpdateText();
        });

        Hide();
    }
}

And this is the script which lists all the available lobbies in the lobby window so that a player can join it.:

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Services.Lobbies.Models;
using UnityEngine;
using UnityEngine.UI;

public class ListAvailableLobbies: MonoBehaviour {


    public static LobbyListUI Instance { get; private set; }



    [SerializeField] private Transform lobbySingleTemplate;
    [SerializeField] private Transform container;
    [SerializeField] private Button refreshButton;
    [SerializeField] private Button createLobbyButton;

    [SerializeField] private Button languageButton;
    [SerializeField] private TMPro.TextMeshProUGUI languageDisplayText;


    private void Awake() {
        Instance = this;

        lobbySingleTemplate.gameObject.SetActive(false);
        refreshButton.onClick.AddListener(RefreshButtonClick);
        createLobbyButton.onClick.AddListener(CreateLobbyButtonClick);
    }

    private void Start() {
        LobbyManager.Instance.OnLobbyListChanged += LobbyManager_OnLobbyListChanged;
        LobbyManager.Instance.OnJoinedLobby += LobbyManager_OnJoinedLobby;
        LobbyManager.Instance.OnLeftLobby += LobbyManager_OnLeftLobby;
        LobbyManager.Instance.OnKickedFromLobby += LobbyManager_OnKickedFromLobby;

    }


    private void LobbyManager_OnKickedFromLobby(object sender, LobbyManager.LobbyEventArgs e) {
        Show();
    }

    private void LobbyManager_OnLeftLobby(object sender, EventArgs e) {
        Show();
    }

    private void LobbyManager_OnJoinedLobby(object sender, LobbyManager.LobbyEventArgs e) {
        Hide();
    }

    private void LobbyManager_OnLobbyListChanged(object sender, LobbyManager.OnLobbyListChangedEventArgs e) {
        UpdateLobbyList(e.lobbyList);
    }

    private void UpdateLobbyList(List<Lobby> lobbyList) {
        foreach (Transform child in container) {
            if (child == lobbySingleTemplate) continue;

            Destroy(child.gameObject);
        }

        foreach (Lobby lobby in lobbyList) {
            Transform lobbySingleTransform = Instantiate(lobbySingleTemplate, container);
            lobbySingleTransform.gameObject.SetActive(true);
            LobbyListSingleUI lobbyListSingleUI = lobbySingleTransform.GetComponent<LobbyListSingleUI>();
            lobbyListSingleUI.UpdateLobby(lobby);
        }
    }

    private void RefreshButtonClick() {
        LobbyManager.Instance.RefreshLobbyList();
    }

    private void CreateLobbyButtonClick() {
        LobbyCreateUI.Instance.Show();
    }

    private void Hide() {
        gameObject.SetActive(false);
    }

    private void Show() {
        gameObject.SetActive(true);
    }

}

I just want the player to be able to filter the lobby by pressing the language button. Please also explain how the player can cycle through all the language options in the enum by pressing this button.

Your response will really matter a lot.

Thank You!

Hi,

Just as you can create lobbies with custom options (CreateLobbyOptions), you can query for lobbies with custom options by using QueryLobbiesOptions.
Calling

await Lobbies.Instance.QueryLobbiesAsync(options);

with custom options will give you the lobbies that you’re searching for.

Query lobbies (see Filtering lobbies section) : Query for lobbies (unity.com)

If you want to query every lobby available, and dynamically display lobbies based on the language, I suppose that you can use QueryLobbiesAsync() with no option, which will give you a QueryResponse object that contains a List of Lobby : from there, you can easily iterate through each Lobby and find the one that contains the data you’re searching for (which would be the local Language).

I hope this helps you, please give more detail if you want anymore help ! :slight_smile:

if you could provide a code snippet or example to clarify the mechanism

I will not be able to test what I’m about to write, so please take this as pseudo-code to improve and correct for your program.

In order to filter lobbies by language, you could try something like : (inside class ListAvailableLobbies at line 64)

foreach (Lobby lobby in lobbyList)
{
    // Retrieve the language DataObject from the Lobby
    if (lobby.data.TryGetValue("LANGUAGE", out DataObject languageData))
    {
        // Do NOT iterate on lobbies that does not correspond to the preferred language
        if (languageData.ToString() != LobbyManager.Instance.Language) continue;
    }

    // here, I'm pasting the lines that I suppose are responsible for creating the UI for a Lobby from your code
    Transform lobbySingleTransform = Instantiate(lobbySingleTemplate, container);
    lobbySingleTransform.gameObject.SetActive(true);
    LobbyListSingleUI lobbyListSingleUI = lobbySingleTransform.GetComponent<LobbyListSingleUI>();
    lobbyListSingleUI.UpdateLobby(lobby);
}

In this code, I suppose that you retrieve the user preferred language by a LobbyManager, but you can really do it in multiple ways - just use one that works for you.
Please tell me if you succesfully made it work, or if you need more help on anything :slight_smile:

OFF TOPIC : I believe your classes ListAvailableLobbies and CreateLobby are supposed to be used as a Singleton. I suggest you to refer to the design patterns e-book from Unity to learn more about useful patterns in Unity and how you can implement them : Level up your code with design patterns and SOLID E-book | Unity