Whole question is really in the title, but I’ll elaborate: Is there any way to get the EntityArchetype of an Entity? Say I have an Entity reference somewhere, and I need to get the actual EntityArchetype of said entity?
Yeah, this would be pretty handy. I have a long list of if (HasComponent) that I could optimize.
I think there is no way to get all components from an entity, none I know of.
I’d welcome a solution for both EntityArchetype for a more optimized routine and another one to get all known comps from an entity, even those that were generated at runtime.
On that regard, what’s the overhead of HasComponent? Is it fast enough we should ignore this kind of optimization?
It is a linear search for all types in the entity’s archetype. I think optimization this small might be negligible?
Anyways a micro-optimization for this is technically possible, by ordering types in an archetype wisely. If the type you want to find is lower in type index number in the archetype, the search returns faster. The one you need to HasComponent often should be added to an entity first.
If you want even more micro optimization, use ComponentDataFromEntity.Exist(entity), you save just one pointer reference to get the type index because the generic T have already been baked in to an index for a CDFE variable.
Isn’t this what EntityManager.GetComponentTypes(Entity entity, Collections.Allocator allocator) does?