Search Unity

Please make it so I can implicitly convert a bool3 into a bool (or add a helper function)

Discussion in 'Entity Component System' started by hcorion, Sep 27, 2020.

  1. hcorion

    hcorion

    Joined:
    Dec 18, 2013
    Posts:
    11
    What I want is to do this

    Code (CSharp):
    1. int3 position;
    2. if (position == new int3(1, 1, 1))
    3. {
    4.     // Do the code
    5. }
    However, I start getting errors because for some reason this produces a bool3, rather than simply a bool. I have to write my own helper function to do this.

    I personally think it makes a lot of sense to be able to implicitly convert a bool3 into a bool. However I can understand if for whatever reason you don't agree.
    If you don't, can Unity please at least add a `.or` and `.and` properties? So at least I can go

    Code (CSharp):
    1. int3 position;
    2. if ((position == new int3(1, 1, 1)).and)
    3. {
    4.     // Do the code
    5. }
    Thanks! I've been really enjoying the new mathematics tools, but this really bugged me.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Code (CSharp):
    1. if (math.all(position == new int3(1, 1, 1))
     
    Baggers_ likes this.
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    I find this a little cleaner

    Code (CSharp):
    1. if (position.Equals(new int3(1, 1, 1))
     
    cj-currie and Baggers_ like this.
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    math.all is the equivalent to "and" and math.any is the equivalent to "or"
     
  5. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    In many cases, you shouldn't be comparing vectors like that because of floating point error. Often you want something like
    math.distancesq(A, B) < 0.01f
    . Of course, there are also situations where equality is perfectly valid (eg if you're setting the values directly from code or you have initial/sentinel values). But occasional little "bumps" like this in floating point code are good oppotunities to check your logic.
     
  6. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    This is only true for floats, ints don't have that issue.
     
    laurentlavigne likes this.