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 Search entity by name? Is there a common solution for this (probably) common problem?

Discussion in 'Entity Component System' started by TarikLarbaoui, Feb 5, 2021.

  1. TarikLarbaoui

    TarikLarbaoui

    Joined:
    Jul 4, 2020
    Posts:
    14
    Each of my entities have a unique name (NativeString64). What's the fastest way to return entitiy Index and Version (The numbers you need to perform an action on a specific entity?) by name? Should I create a separate orderedlist <name, struct{ID,Index}> ordered by name so that it's faster to search through it or does EntityQuery already do it better and faster?

    Thank you!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,541
    Searching entities by a name, is probably the worse of possible options.
    Not only you utilize a strings, which can produce GC, but you need iterate each entity, just to find the one you need.

    You may reconsider design, if you needing search by a name.
    You should rather focus on searching by ID, if anything.
    Alternatively, you can put names in enum and cache enum name-entity in hashamap.

    But I suspect, you are doing something odd-ish.
    So my question is, what is your use case, for needing searching by a name?
     
    Nyanpas likes this.
  3. TarikLarbaoui

    TarikLarbaoui

    Joined:
    Jul 4, 2020
    Posts:
    14
    Thanks for answering! My use-case is a "Twitch Plays" game.
    Each player has a character. Whenever he enters a command in chat, his character has to react. I highlighted the kind of input I have parsed and now have to map to entities in the screenshot above.
    So, unless I'm missing something, each time a twitch user writes something, I have to look up which entity he is.

    Thanks again for your help
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,694
    In the unity tinyTime example they look for the TimeText entity by name
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,694
    I should note I agree with Antypodish I don’t think it is good practice ( I’m no pro) , I think it’s a work around in the tinyTime example
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,937
    If you are unsure about performance when using DOTS, first do it the easy and naive way that is compatible with Burst, and then if performance isn't good enough, come back with number of entities, number of names to match, ect. For something like this, what is fastest will depend a lot on your data.

    As several of the others have mentioned here, in DOTS it is usually best to avoid strings and look things up by IDs and whatnot. However, for your use case, I don't think you can actually get away from strings. That doesn't mean that you are doing it wrong, it just means that your use case is uncommon.
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,541
    What I would do, I would cache commands and user names in two separate hash collections.
    Key as string and value as command / user ID.
    Then, you can retrieve ids rapidly, when strings are valid.
    Your command!/ user ID can be either int, or stored entity index and version.
    Then 6ou just operate on these references, rather strings itself.

    Does user need to know commands and need to type them? Or there are shortcut buttons, to insert commands? If the second, then you won't need to really worry about strings, as button could call just command ID. Otherwise you need a vqlidator and string parser.
     
    KarelA likes this.
  8. jasons-novaleaf

    jasons-novaleaf

    Joined:
    Sep 13, 2012
    Posts:
    181
    Why not just a Dictionary<string,Entity> ?
     
  9. TarikLarbaoui

    TarikLarbaoui

    Joined:
    Jul 4, 2020
    Posts:
    14
    Are dictionaries ordered by their key? I'd love to have a lookup time of log(n)
     
  10. TarikLarbaoui

    TarikLarbaoui

    Joined:
    Jul 4, 2020
    Posts:
    14
    I'm looking up Unity's Hashtable right now, if that's what you meant by hash collection. Not sure I see the difference between that and a dictionary. Ya they need to know the commands and type them. I haven't dove deep enough in twitch's API to add buttons. Thanks for your help! Very much appreciated.
     
    Antypodish likes this.
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,541
    You got it.

    The main difference is, hashmaps are friendly for DOTS, and burst as long are blittable.

    Otherwise, they are similar in functionality.
     
    Nyanpas likes this.