Research within objects by property

Hello,

I have different objects on a level, each of them having custom properties like “firstname”,“job”,“sex”. I would like to create a search bar, so if a player types something in it, it would check for all objects’ properties to see if there are some matches and to return matching objects.

For exemple, typing “Ma” in the research should return names of every object that has “Male” as sex, or “Marketer” as job or “Mary” as name, etc.

I believe that for the research I just have to do a loop that checks values of each object properties and, if there’s a match, returs the object’s name. But how could I workaround approximate matches like “Ma” → “Mary” and “Mory” → “Mary”?

The first case “Ma” → “Mary” can be found with a simple string.Contains or Regex.Match

The second, “Mory” → “Mary”, needs something more complex. Please, look the first answer here, it should put you on the road.

If you have too may objetcs to search, I don’t recommend you relying only on reflection for make the property search, I think is best to have some type of cache for the values of the properties which will be searched.

You can, for example, store the values in a Dictionary<string, object> in the property setter of the involved classes.

with regard to the “mory->mary” part, take a look at SoundEx (the instructions will be available online).
Soundex is an algorithm that takes a string and removes the vowels and doubled letters, and converts the remaining letters into a special string which you store in a separate property to the original string.

Then, when the user searches, you do the same thing to his search term and compare that to the stored soundex strings as well as doing your standard .contains check on the real strings.
e.g. “Mary” and “Mory” both soundex to “M600”

It’s a well established system, and there are standard c# functions for doing it, I don’t know if they’re available in unity or whether you might have to write your own (its not hard)