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

Alternative to boolean in IComponentData - Blittable Datatypes

Discussion in 'Entity Component System' started by Spy-Shifty, Mar 28, 2018.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Hi there,

    what type would you use instead of boolean in an IComponenData component.

    Because of the fact that bool isn't a blittable type, you have to use a different datatype for this.
    https://msdn.microsoft.com/de-de/library/75dwhxf7(v=vs.71).aspx

    One aspect would be to use byte because of its size.
    Anyway the handling isn't that beautiful...

    Maybe a struct that capsules a byte and some methods for conversion to bool will help

    Any sugesstion on that?
    What do you think?
     
  2. Deleted User

    Deleted User

    Guest

    Unity.Mathematics.bool1 should work
     
    RaL, Singtaa, Tzelon and 1 other person like this.
  3. Xisor

    Xisor

    Joined:
    Jan 30, 2016
    Posts:
    8
    jeez, bool1 was removed from the latest Mathematics, so currently i don't see any workarounds shipped with library. Looking back into old C days when everyone was making its own bool types everywhere :)
     
    vanxining likes this.
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    To use `bool1` with `IComponentData` you have to stay with "preview.2".

    - "preview.4" removed `bool1` to be in line with float float2 float3.... but without making `bool` blittable.
    - "preview.3" crashes everything saying something about reflection even in a scene without any systems. The world bootstrap does not work.

    https://forum.unity.com/threads/bool1-gone-in-preview-4.532814/
     
  5. Xisor

    Xisor

    Joined:
    Jan 30, 2016
    Posts:
    8
    ty for the link. Ok will wait for the blittable bool support, currently im living with simple byte-size wrapper like
    Code (CSharp):
    1.     public struct TBool
    2.     {
    3.         private readonly byte _value;
    4.         public TBool(bool value) { _value = (byte)(value ? 1 : 0); }
    5.         public static implicit operator TBool(bool value) { return new TBool(value); }
    6.         public static implicit operator bool(TBool value) { return value._value != 0; }
    7. }
     
  6. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    246
    Great job and thank you!

    I've wrote an inspector for this one

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [CustomPropertyDrawer(typeof(TBool))]
    3. class TBoolDrawer : PropertyDrawer
    4. {
    5.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    6.     {
    7.         var field = property.FindPropertyRelative("_value");
    8.         field.intValue = EditorGUI.Toggle(position, label, field.intValue != 0) ? 1 : 0;
    9.     }
    10. }
    11. #endif