bool1 gone in preview 4?

Upgraded to .12preview4. And it seems Unity.Mathematics no longer have bool1.

correct. It was inconsistent with float2/3/4 etc

Cool. So what should we use in its place? Although everything is in preview, but for code using bool1, this should be considered a breaking change. Am I missing something, or is there already another blittable bool we can use for IComponentData?

I’m pretty certain bool1 was basically a hack around an earlier issue with the burst compiler, as I don’t think it natively mapped bool well. I assume the restricition was lifted and we can now use plain bool, is this correct @Joachim_Ante_1 ?

Right I thought the same. But the regular bool is currently not usable in IComponentData in preview 4.

Wow, I would hate it if I have to store ints for bool…

Currently bool is considered blittable by C#. For Native containers we are going to change the definition of blittable to be so that bool is assumed to be blittable.

When will it be possible to use normal bools in IComponentData struct?
The check seems to come from com.unity.collections but this package is not in the package manager, is it shipped with the editor?

Meanwhile, if anyone needs a bool:

public struct Bool
{
    private byte value;

    public static implicit operator Bool(bool b)
    {
        return new Bool() { value = System.Convert.ToByte(b) };
    }

    public static implicit operator bool(Bool b)
    {
        return b.value == 1;
    }
}
10 Likes

I just started with ECS and this hung me up for bit before I found this thread. I would have assumed that base bool would have been blittable by the ECS like float/int is.

Officially the first brick wall everyone has to run into.

4 Likes

Is this still the plan?

6 Likes

Is that fixed?

Yep, just encountered it. Super unobvious, and not mentioned anywhere.

In case anyone needs a proper drawing / serialization for the entity (e.g. for entity templates / prefabs), here’s a custom drawer for the Bool:

using UnityEditor;
using UnityEngine;
using Utility;

// ReSharper disable once CheckNamespace
namespace UtilityEditor {
    [CustomPropertyDrawer(typeof(Bool))]
    public class BoolDrawer : PropertyDrawer {
        public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) {
            EditorGUI.BeginProperty(position, label, prop);

            SerializedProperty value = prop.FindPropertyRelative("_value");

            position = EditorGUI.PrefixLabel(position, label);

            EditorGUI.BeginChangeCheck();
            
            bool currentVal = EditorGUI.Toggle(position, value.boolValue);

            if (EditorGUI.EndChangeCheck()) {
                value.boolValue = currentVal;
            }

            EditorGUI.EndProperty();
        }
    }
}

Don’t forget to modify the Bool to support serialization:

using System;
using UnityEngine;

namespace Utility {
    /// <summary>
    /// Workaround for the bool not being blittable
    /// </summary>
    [Serializable]
    public struct Bool {
        [SerializeField]
        private byte _value;

        public static implicit operator Bool(bool b) {
            return new Bool {_value = Convert.ToByte(b)};
        }

        public static implicit operator bool(Bool b) {
            return b._value == 1;
        }
    }
}

This thread was in May? Now I can really feel how much time ECS rework of my game costs. (worth it anyhow)

I am sure everyone has his own flavor bool at this point… This one is 1-file edition with the drawer bundled, but essentially about the same as yours E7ECS/DataStructure/BlittableBool.cs at master · 5argon/E7ECS · GitHub

3 Likes