- Home /
Array of named booleans
If I create a boolean array:
public bool[] boolArray;
Then the inspector shows me boolean tick-boxes named "Element 0, Element 1" etc. which isn't really helpful.. how can I change it so that each element in the array has an actual name?
There is no grammar for that in C#. Array elements share a common name (in this case: boolArray) and are differentiated from each other by their index. You can write boolArray[3] in your code, but you can't write boolArray[name] or boolArray.name.
(well, actually, boolArray[name] could work if you used enums - but it still wouldn't show up in the editor)
If each element in the array actually means something specific, you might want to create a class of named bools, such as:
public class $$anonymous$$yBools { public bool isCorrect; public bool isImportant; public bool isUrgent; public bool isGiraffe; // etc. } public $$anonymous$$yBools boolArray;
Or, you could create an array of simple objects:
public class NamedBool { public string Name; public bool Value; } public NamedBool[] boolArray;
Note that this creates serious overhead, because a string can easily be many times bigger in terms of memory usage than a bool.
There is also a library class in C# called Dictionary, and it does a similar thing, but it's most likely a bit of an overkill in this case.
I guess you could also try some advanced custom editor magic and create custom editor interface that provides the na$$anonymous$$g functionality. It would probably use a data structure like the second example, only you wouldn't need to keep names in memory at runtime.
Well seeing as both me and @Jacek_Wesolowski have suggested the NamedBool class object option, I would recommend that method above any other.
I wouldn't. :) A $$anonymous$$or coupling of data like that shouldn't be a class, it should be a struct. But that's not the main reason. I think this should be a Dictionary because it makes lookups more elegant when you want to access the data later. It's prettier and more succint to access an array with a string and get a boolean, than having to loop through a collection of NamedBoolean classes/structs and return when you find one that matches your search string.
This should be a struct, but structs didn't show up in the inspector last time I checked.
The rest depends on the usage pattern. From what I gather, the names are needed for user's convenience and won't be needed at runtime. There is no faster way to access an array element than its index. Dictionary saves time if you plan on searching for a small number of values in a large set.
What I'm essentially playing around with is a card game. I actually tried making a class, unfortunately I couldn't find a way to loop through each variable in the class to check which ones are enabled. Here's what I tried (and failed) to use:
[Serializable]
public class CardTypes : IEnumerable
{
public bool _monster = false;
public bool _equipment = false;
public bool _land = false;
public bool _spell = false;
}
I think I'm probably going to try the Dictionary that @Christian H Pedersen suggested, but thanks for all the info!
Answer by CHPedersen · Mar 01, 2012 at 03:07 PM
It sounds like you might need a Dictionary instead. A Dictionary is a datastructure that allows you to link a key to the values that go into the structure. A typical use case is a phonebook, where you link the name of a person (the key is then a string) to his/her phone-number (the value is then an integer).
Try something like this:
Dictionary<string, Boolean> NamedBooleans = new Dictionary<string, bool>();
NamedBooleans.Add("This is the name of the first boolean", false);
NamedBooleans.Add("This is the name of the second boolean", true);
Then, later on, you can look up the booleans like this, for example:
if (NamedBooleans["This is the name of the first boolean"])
{
// Yada yada
}
Answer by Caiuse · Mar 01, 2012 at 03:04 PM
As far as I am aware you can't assign a name to a boolean, there may be a way of serializing the boolean with a name but I'm not sure.
1) use a Hashtable to store the booleans:
var hashtable = {};
function Start(){
hashtable["My boolean"] = true;
hashtable["Another boolean"] = false;
}
then you can refer to them now like so...
if(hashtable["My boolean"]){
//do stuff
}
2) make class object for a named boolean:
var array : NamedBoolean[];
class NamedBoolean {
var name : string;
var bool : boolean;
}
with a class object they will show in the inspector as the "name" variable you assign.
refer to them like so:
if(array[i].bool){
//do stuff
}
3) similar to a hashtable you could use a generic list
var myList = new List.<boolean>();
then you can refer to them now like so...
if(myList["My boolean"]){
//do stuff
}