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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

boolean (Blittable) in IComponentData struct?

Discussion in 'Entity Component System' started by Antypodish, Aug 2, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    According to ms documentation, Boolean is none-blittable data type.
    Code (CSharp):
    1. https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types
    Blittable data types

    Therefore, bool data types can not be part of data component struct, with IComponentData.
    How can I use booleans then, avoiding using for example bytes, or ints?
    Or can I?
     
    deus0 likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    deus0 and Antypodish like this.
  3. jamiebrynes

    jamiebrynes

    Joined:
    Sep 12, 2017
    Posts:
    18
    You have to write a wrapper struct, in my experience. But you can write implicit conversions so that its fairly seamless to use.

    Code (CSharp):
    1.    
    2. public struct BlittableBool : IEquatable<BlittableBool>
    3.     {
    4.         private byte value;
    5.  
    6.         public BlittableBool(bool value)
    7.         {
    8.             this.value = Convert.ToByte(value);
    9.         }
    10.  
    11.         public static implicit operator bool(BlittableBool blittableBool)
    12.         {
    13.             return blittableBool.value != 0;
    14.         }
    15.  
    16.         public static implicit operator BlittableBool(bool value)
    17.         {
    18.             return new BlittableBool(value);
    19.         }
    20.  
    21.         public bool Equals(BlittableBool other)
    22.         {
    23.             return value == other.value;
    24.         }
    25.  
    26.         public override bool Equals(object obj)
    27.         {
    28.             if (ReferenceEquals(null, obj))
    29.             {
    30.                 return false;
    31.             }
    32.  
    33.             return obj is BlittableBool && Equals((BlittableBool) obj);
    34.         }
    35.  
    36.         public override int GetHashCode()
    37.         {
    38.             return value;
    39.         }
    40.  
    41.         public static bool operator ==(BlittableBool left, BlittableBool right)
    42.         {
    43.             return left.Equals(right);
    44.         }
    45.  
    46.         public static bool operator !=(BlittableBool left, BlittableBool right)
    47.         {
    48.             return !left.Equals(right);
    49.         }
    50.  
    51.         public override string ToString()
    52.         {
    53.             return ((bool) this).ToString();
    54.         }
    55.     }
     
    starikcetin and Antypodish like this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Thx. I need admit, I missed that one.
    In the link, there are some interesting suggestions.

    This looks great. Few nice methods included.
    But in my opinion, this a bit misses the point of having bool via wrapper, if trying to gain on performance, since it is adding extra operation, just to get a bool. But I may be just overreacting :)
    I presume, I would be better stick in this case with bytes?
    At least for now.
     
    LaneFox likes this.
  5. Fabrice_Lete

    Fabrice_Lete

    Unity Technologies

    Joined:
    May 5, 2018
    Posts:
    49
    Your best option is byte for now.
     
    deus0 and Antypodish like this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Thx Fabrice_Lete, I shall do that for time being.
    If bools ever come to light, it will be easier to track bytes down, to replace.
     
  7. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    It's been 4 years and omg, I just realized its now blitable.. lmao this is why I need friends
     
    apkdev likes this.