Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Dictionary get names from partial search

Discussion in 'Scripting' started by OutDoorsScene, Sep 4, 2023.

  1. OutDoorsScene

    OutDoorsScene

    Joined:
    Sep 9, 2022
    Posts:
    140
    I recently saw CS2 console and decided to improve my own console. I am trying to use a Dictionary to autocomplete all available commands i.e. typing "s" would display "survey","skills","slow"


    Commands and Dictionary are added to a list.
    Code (CSharp):
    1.  
    2.  private void Awake()
    3.     {
    4.         devConsole = GetComponent<DevConsole>();
    5.         CreateConsoleCommands();
    6.     }
    7.  
    8. void CreateConsoleCommands()
    9.     {
    10.         AllCommands = new List<DevCommands>();
    11.         SearchCommand = new Dictionary<string,DevCommands>();
    12.  
    13.         string CommandName = "help";
    14.         DevCommands Help = new DevCommands(CommandName, "Shows all commands.", () =>
    15.         {
    16.             foreach (var item in AllCommands)
    17.             {
    18.                 DefaultLog(item.Name + ": " + item.Description);
    19.             }
    20.  
    21.         });
    22.         AllCommands.Add(Help);
    23.         SearchCommand.Add(CommandName, Help);
    24.  
    25.         CommandName = "clear";
    26.         DevCommands Clear = new DevCommands(CommandName, "Clears the console", () =>
    27.         {
    28.             ConsoleLogText.text = string.Empty;
    29.  
    30.         });
    31.         AllCommands.Add(Clear);
    32.         SearchCommand.Add("clear", Clear);
    33.  
    34.         CommandName = "spawn";
    35.         DevCommands Spawn = new DevCommands(CommandName, "spawns items ", () =>
    36.         {
    37.             if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("GAME"))
    38.             {
    39.  
    40.                 SpawnProps.SpawnAll();
    41.  
    42.                 DefaultLog("Spawned more props.");
    43.             }
    44.             else
    45.             {
    46.                 DefaultLog("Cannot be used in the main menu.", LogType.Warning);
    47.             }
    48.  
    49.         });
    50.         AllCommands.Add(Spawn);
    51.         SearchCommand.Add(CommandName, Spawn);
    How would I be able to do a partial search for a string?
    Code (CSharp):
    1.  void SearchForDevCommand(string text)
    2.     {
    3.         if (SearchCommand.TryGetValue(ConsoleInputField.text,out DevCommands command))
    4.         {
    5.             Debug.Log(command.Name);
    6.         }
    7.         else
    8.         {
    9.             Debug.Log("Can't find any command.");
    10.         }
    11.     }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Not exactly sure, as I've never done this, but "string starts with" is ringing in my head for some reason.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    Some approaches:

    - using the StartsWith method on the string
    - regular expressions
    - using IndexOf on the string (more for searching than prefixing)

    See docs for details on all the above... none of it has to do with Unity.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,750
    Don't use a dictionary, since they only supports searches for exact keys. Use a SortedList (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedlist-2?view=net-7.0) then implement a binary search with a custom comparer (https://stackoverflow.com/questions/967047/how-to-perform-a-binary-search-on-ilistt) that finds the index of the first key that starts with the the search string, then walk through the lists until you reach a key that doesn't match the criteria.

    If you don't want to write your own binary search algorithm, you can also use the dictionary plus a plain List with the command names. Sort the list, then call List.BinarySearch (https://learn.microsoft.com/en-us/d...ions.generic.list-1.binarysearch?view=net-7.0) passing a custom comparer function that uses string.StartsWith.

    But unless you have over a 1000 commands, you don't need to bother with the binary search and can just do a normal linear search on the list. The most important detail is to make sure the list is sorted alphabetically, this way all commands that start with the same string will be next to each other in the list.
     
    Bunny83 likes this.