- Home /
List of bools with names
So I want to make a list of bools, with names associated to it
Ideally I want to access it like a class
myList.someNamedBool = true;
I could do that with a simple class, however I want the functionally of the list, so I can put it through a for/foreach loop?
I thought about making a simple list of bools and then use an enum to name them, but I like to know if there is a more elegant way?
thanks in advance.
UPDATE:
This is my setup so far, can it be made simpler?
[System.Serializable]
public class traitClass
{
public bool isTrue=false;
public string name;
}
public List<traitClass> traitBools;
private void Start()
{
traitBools.Add(new traitClass { Name = "workHorse" });
traitBools.Add(new traitClass { Name = "lazy" });
traitBools.Add(new traitClass { Name = "intelligent" });
traitBools.Add(new traitClass { Name = "dumb" });
traitBools.Add(new traitClass { Name = "brawler" });
traitBools.Add(new traitClass { Name = "coward" });
traitBools.Add(new traitClass { Name = "perfectionist" });
traitBools.Add(new traitClass { Name = "bungler" });
traitBools.Add(new traitClass { Name = "loyal" });
traitBools.Add(new traitClass { Name = "iloyal" });
traitBools.Add(new traitClass { Name = "energetic" });
traitBools.Add(new traitClass { Name = "sluggish" });
bool dumb = traitBools.Find(x => x.Name.Equals("dumb")).Is;
foreach (traitClass trait in traitBools)
{
}
A Dictionary would provide a much simpler structure and an easier access.
public List<traitClass> traitBools;
private void Start()
{
traitBools.Add("workHorse", True);
traitBools.Add("lazy", True);
traitBools.Add("intelligent", False);
traitBools.Add("brawler", False);
traitBools.Add("coward", True);
traitBools.Add("sluggish", True);
if (traitBools.TryGetValue("lazy", True))
{
Debug.Log("I am a lazy developer");
}
}
is there a way to do this that works in the inspector?
Answer by Mouton · Sep 26, 2019 at 10:56 PM
You are looking for a dictionary. It is a collection of generic key/value pairs. You can use a string or an enum for the key, and a bool for the value.
public class Example
{
public Dictionary<string, bool> Triggers = new Dictionary<string, bool>();
private void Start()
{
Triggers.Add("SwitchA", True);
Triggers.Add("Switch", False);
foreach (KeyValuePair<string, bool> trigger in Triggers)
{
trigger.Value;
}
}
}
Helpful resources
List and Dictionaries in Unity (beginner tutorial): https://unity3d.com/fr/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries C# Dictionaries: https://docs.microsoft.com/fr-fr/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8
The problem with a dictionary is that its used to store connected data in a flat structure, what I want is hierarchically, so that it is easier for me to access the bool using the name, without the need to implement a mechanic to search the list each time i need the bool value
With a dictionary, you can access element by keys.
public class Example
{
public Dictionary<string, bool> Triggers = new Dictionary<string, bool>();
private void Start()
{
Triggers.Add("A", True);
Triggers.Add("B", False);
foreach ($$anonymous$$eyValuePair<string, bool> trigger in Triggers)
{
trigger.Value;
Debug.Log($"{trigger.$$anonymous$$ey}: {trigger.Value}");
}
trigger["isReady"] = false;
}
}
You can use enum ins$$anonymous$$d of strings
public class Character
{
public enum Trait {
WorkHorse,
Lazy,
Intelligent,
Dumb,
Brawler,
Perfectionnist,
Bungler,
Loyal,
Iloyal,
Energetic,
Sluggish,
}
public Dictionary<Trait, bool> Traits = new Dictionary<Trait, bool>();
private void Start()
{
Traits.Add(Trait.Lazy, true);
Traits.Add(Trait.Loyal, true);
Traits.Add(Trait.Brawler, false);
foreach ($$anonymous$$eyValuePair<Trait, bool> trait in Traits)
{
Debug.Log($"{trait.$$anonymous$$ey}: {trait.Value}");
}
Traits[Sluggish] = true;
bool isDumb = Traits.Find(x => x.Dumb);
}
}
Although, this is less useful since enums cannot be created from the editor and you have the capacity to add one dynamically. You can store any type in the dictionary, so it can be a class too if you want to achieve what you are calling a "hierarchical" structure.
I ended up with something similar to this, your however is a bit more refined, thank you for you effort!
Uhm, what do you mean by hierarchical? All you said is you want a collection of string names which are associated with a boolean value. That's exactly what a Dictionary does in the most efficient way. The access of any of the items is essentially O(1) in complexity. You don't have to search for a value. If you know that a certain key is in the dictionary, you can simply do
Triggers["workHorse"]
and you get back the associated value with that key. Though if you are not sure if a certain key exists, it's more common to use "TryGetValue".
In the case of a Dictionary<string, bool>
that would be
bool val;
if (Triggers.TryGetValue("workHorse", out val))
{
// Yes, the key "workHorse" does exist and it's value is "val"
}
Of course if you want to combine several attributes / fields together you can still use some kind of "Trait" class.
Iterating through a dictionary with foreach is probably something you almost never do and therefore a bad example in this case. A dictionary is not "orderd". It uses the hash code of the key to distribute all the values into "buckets".