It seems that T? is not blittable, ie, the following will throw a blittable validation exception. Code (CSharp): struct MyData : IComponentData { public int? value; // not blittable } I've written simple replacement, but it'd be great to not require this. Code (CSharp): struct MyData : IComponentData { public Opt<int> value; // blittable } /// <summary> /// T? simulation that can be used in IComponentData /// </summary> public struct Opt<T> { T value_; public Bool HasValue; public T Value { get { if (!HasValue) throw new InvalidOperationException("!HasValue"); return value_; } } public static implicit operator bool(Opt<T> opt) { return opt.HasValue; } public static implicit operator Opt<T>(T value) { return new Opt<T> { value_ = value, HasValue = true, }; } }
int? is a shorthand for Nullable<int> and, as you can see on reference source, Nullable<T> has a boolean and a T value, and as you can see here also, boolean is not a blittable type, so in order to a struct be blittable, all its members must be blittable. Why boolean isn't blittable? Because the way it is stored internally on memory vary on managed and non-managed code and blittable types must be stored on the same way, be it managed or non-managed.
nullable types (T? / Nullable<T>) are actually value types, consisting in the value + a 'has value' flag. then the compiler does it's magic so "x == null", assignment and operators work. the problem is that Nullable<T> contains a bool, and bool is not blittable.
Yes, my mistake, sleepy wrote nonsense Yes of course Nullable is a value type (structure as in any way)
Unity is making so that bool is considered blittable. (https://forum.unity.com/threads/bool1-gone-in-preview-4.532814/#post-3508456) By then it might be usable in a native container. Also I noticed that blittable check code is not in the Entities package, this change would probably have to come with the main Unity update. You could probably also copy C#'s Nullable<T> code (https://referencesource.microsoft.com/#mscorlib/system/nullable.cs) and replace with your blittable bool.
Unity doesn't allow some things even when they are blittable it seems. Such as arrays of blittable types which are supposed to be blittable, or even their own Collections.
Arrays are not blittable @Necromantic. Regardless of the stored type, the array itself is not blittable. https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types
Well, arrays are blittable. I guess the second part is the problem, making any struct containing them unblittable.
Ah, right, when Unity fixes the bool IsBlittable check hopefully this will work. Unity's definition of blittable is different from the .NET definition. For Unity, anything that holds managed memory is not blittable.
Looks like arrays are being addressed: https://forum.unity.com/threads/unity-berlin-2018-talk.537082/