-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v4] Implement component id lookup support for System.Type #27
Comments
I messed around with trying to implement this and it seems like there's no way to get the size of the component without breaking NativeAOT. You can construct an instance of T using Activator as long as you annotate the type properly. However, there's no way to size it directly since you can't access [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] You might be able to hack some sort size estimator by walking the fields and using known sizes for base types but that would get very messy. Might be out of scope but you could consider splitting into a Flecs.Core package with AoT support and a Flecs package without that adds support for dynamic registration and types. Alternatively you could use defines for gating off non-aot compatible code. |
Worth noting, it seems Type currently isn't AoT compatible. Its usage of GetEnumValues throws an AoT warning. Adding <PublishAoT>true</PublishAoT> to the Examples project and running
|
I wonder if switching to Enum.GetValuesAsUnderlyingType fixes AOT compatibility. I dropped support for versions below .NET 8 in the type-safe-queries branch so we have access to this function now. |
Yup that fixes it! AoT still gives off a warning about the if (RuntimeFeature.IsDynamicCodeSupported)
{
FieldInfo[] fields =
type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
size = fields.Length == 0 ? 0 : size;
alignment = size == 0 ? 0 : alignment;
} but seems fine since its gated. You could annotate it but it looks like you'd have to bubble up that annotation to 50+ other generic methods that call Type<T> |
In the meantime, would you have any qualms with storing the reflection Type on the component entity? I have a usecase for going from the component entity Id to knowing its type for runtime reflection. I figure easiest solution would be to just do world.Entity(component).Set(typeof(T)); in Type::RegisterComponent |
See #41 |
It would be useful in reflection scenarios to allow the user to lookup component ids and execute ECS operations using
System.Type
. This functionality is required for automatic member registration/serialization/deserialization support. Needs to also support NativeAOT with trimming which might not be possible with the way component registration currently works.The text was updated successfully, but these errors were encountered: