Search Unity

How do you simplify this script

Discussion in 'Scripting' started by Omygoodnessgames, Jan 12, 2018.

  1. Omygoodnessgames

    Omygoodnessgames

    Joined:
    Oct 21, 2017
    Posts:
    2
    Is there any way to simplify this script???

    if (x[0].istrue == false)
    {
    if (x[1].istrue == false)
    {
    if (x[2].istrue == false)
    {

    }
    }
    }
    else
    {

    }
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Collating results via a for loop or magic method would not yield any simplification unless you're lying and there's more to it.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Right now you have nested ifs.

    While you can shorten the ifs to

    if(!x[0].istrue) for example, beyond that there is no context in what is suppose to happen.

    You might be able to do a loop still, where you set a bool to false before the loop, loop through and if you hit true, set the bool to true and break, then handle whatever based on that bool, but again, with nested if statements and only this much info, it is difficult to say.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    There is no way to improve what you posted without more information on the goal of the code.
     
  5. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    I suppose you meant more than 3 variable comparisons...in that case, you could use a for loop but really depends on what you have to do.
     
    TaleOf4Gamers likes this.
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    Technically it does nothing, so you could simplify it by deleting it.
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. //your current code
    2. if (x[0].istrue == false)
    3. {
    4.     if (x[1].istrue == false)
    5.     {
    6.         if (x[2].istrue == false)
    7.         {
    8.             Debug.Log("x[2] is false");
    9.             //do some code here
    10.         }
    11.     }
    12. }
    13. else
    14. {
    15.     Debug.Log("x[0] is true");
    16.     //do some code here
    17. }
    18.  
    19.  
    20. //no one knows what you're after, so here is my 2 cents.
    21. for(int i = 0; i < x.Length; i++)
    22. {
    23.     if(!x[i])
    24.     {
    25.         Debug.Log("x[" + i + "] is false");
    26.     }
    27. }
     
    Omygoodnessgames likes this.
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I suppose one way to simplify is to use (!x[0].istrue) instead of (x[0].istrue == false).
     
  9. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    If you want it shortened to ridiculousness, there's always this:

    Code (CSharp):
    1.         if (!(x[0].istrue | x[1].istrue | x[2].istrue))
    2.         {
    3.             Debug.Log("All false!");
    4.         }
    5.  
     
    ihgyug likes this.
  10. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    I'll see your ridiculousness and raise you:

    Code (CSharp):
    1. Debug.Log(((x[0].istrue | x[1].istrue | x[2].istrue) ? "Not a" : "A") + "ll false!");
     
    ihgyug likes this.