- Home /
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"?
I guess I will use tags to tag every concerned object as "searchable", then each time a user types a letter in the searchbar, I will do a loop that looks for those properties' values to compare them to the user typed keyword.
Inside the lopp, I will have to perform an "Affinity check" function that would return true for searches like "$$anonymous$$a = $$anonymous$$ary".
Answer by jovino · Jul 04, 2013 at 01:02 PM
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.
Thank you for the first case, Thank you for the second yet I just had read few articles about Levenshtein so I was already on the good way.
Concerning the optimization: I don't kinow if I understood well, but do you suggest to create a side "container" that should store a kind of database like "Object - Property1 value - property 2 value, etc"?
Thank you much
I was thinking something like this, but your idea of a static container it's a good one for me too:
using System.Collections.Generic;
interface ISearchable
{
object SearchValue(object valueToSearch);
}
public class SearchableObject : ISearchable
{
private Dictionary<string,object> properties;
public SearchableObject ()
{
properties = new Dictionary<string,object>();
}
public object SearchValue (object valueToSearch)
{
if(valueToSearch as string == null)
return null;
foreach(object propValue in properties.Values)
{
if(propValue.ToString().Contains(valueToSearch as string))
return propValue;
}
return null;
}
protected void SetValue(string propName, object propValue)
{
if(this.properties.Contains$$anonymous$$ey(propName))
this.properties[propName] = propValue;
else
this.properties.Add(propName, propValue);
}
}
public class Car : SearchableObject
{
private float engine;
public float Engine
{
get { return engine; }
set
{
SetValue("engine", value);
engine = value;
}
}
}
Still don't know how to thank you enough, I just reached the supersonic speed in my project's development :)
Answer by Styxxx · Jul 04, 2013 at 02:40 PM
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)
Your answer
![](https://koobas.hobune.stream/wayback/20220613105446im_/https://answers.unity.com/themes/thub/images/avi.jpg)