Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mikawendt · Sep 28, 2019 at 02:35 PM · listclassbool

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)
         {
 
         }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mouton · Sep 28, 2019 at 03:06 PM 0
Share

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");
     }
 }
avatar image darrelltstevens · Oct 17, 2020 at 07:02 AM 0
Share

is there a way to do this that works in the inspector?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mikawendt · Sep 26, 2019 at 11:37 PM 0
Share

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

avatar image Mouton mikawendt · Sep 28, 2019 at 02:59 PM 2
Share

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.

avatar image mikawendt Mouton · Oct 03, 2019 at 12:04 PM 1
Share

I ended up with something similar to this, your however is a bit more refined, thank you for you effort!

Show more comments
avatar image Bunny83 mikawendt · Sep 28, 2019 at 03:38 PM 1
Share

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".

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How can i store the Amount of an item i have in an inventory? 0 Answers

How do I Create 5 Random Racers? 2 Answers

Integer List values don't get initialized in Constructor of Class 2 Answers

Can I make a list of hashtables or classes with pragma strict 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges