Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question A lot of if's

Discussion in 'Scripting' started by YahManGames, Apr 1, 2021.

  1. YahManGames

    YahManGames

    Joined:
    Sep 27, 2018
    Posts:
    3
    Hi there,
    I want use a long list of bools, that exist in a master list and in an array of units. And I am trying to think how to best manage this.

    So, for example, the Master List would contain this:
    Code (csharp):
    1.  
    2. public bool isRed;
    3. public bool isGreen;
    4. public bool isYellow;
    5. public bool isBlue;
    6.  
    ///this seems like not much to manage but if I have a 100 booleans this becomes a task as each unit, needs to know if itself is any of these things, then pass that to the manager, to know which types exist in a level.

    For example:
    each unit at start:
    Code (csharp):
    1.  
    2. void passInfoToMasterList(){
    3.  
    4. if(isRed){
    5. MasterList.isRed = true;
    6. }
    7. if(isBlue){
    8. MasterList.isBlue = true;
    9. }
    10. if(isGreen){
    11. MasterList.isGreen = true;
    12. }
    13.  
    14.  
    15. }
    16.  


    ...again, if I have a 100 boolean, this becomes an task.
    How might you create a system that would account for this, in code, much simpler. With out all the ifs.?Thanks
    *note was thinking dictionaries and lists? I dont know, but if you do, thanks!
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It looks like the bools describe distinct properties of the object. If whatever you are building (you aren't telling us what you are trying to achieve) has it so that only one of those booleans can ever be true (i.e. they are mutually exclusive, if they are one they can't be the other), you can simplify that and and create an enumeration like so:

    Code (CSharp):
    1. enum myColor {red, green, blue};
    and then simply contain the color information (and all other that are mutually exclusive) into one variable.
     
    Bunny83 likes this.
  3. YahManGames

    YahManGames

    Joined:
    Sep 27, 2018
    Posts:
    3



    Sure, how would you manage an enum? I am not familiar. Consider that at each stage a question is asked (example "is object red?").... at that point, all non red objects are removed. Subsequently, so is the question : is object red?").... its just a management system with many conditions, that get removed to reveal the true identity of the target object.

    Like Clue, "the killer was Professor Plum, in the library with the wrench".

    So, I am looking for a little less labour intensive way to manage this in code. Thanks
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    In clue, we have multiple sets of mutually exclusive choices. Only one weapon out of fixed a set of weapons, one room out of a fixed set of rooms, and one person out of a set of persons.

    so you would define an enum for each:

    Code (CSharp):
    1. enum murderWeapon {wrench, rope, poker, knife, pillow};
    2. enum room {library, study, hall, vestibule, dining};
    3. enum suspect{Plum, Hogan, Smith, Jones};
    With this you have three variables, one each for murder suspect, murder weapon and room, and each of these can only hold one value that automatically excludes all other possible values.