Search Unity

ComponentData by interface

Discussion in 'Entity Component System' started by Tony_Max, Dec 8, 2018.

  1. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    I need to access some ComponentData that match to interface. I just can not find a way to do it, but it seems that there is must be a way.

    For example I have structs
    Code (CSharp):
    1. struct MyComponentA : IComponentData, IMyInterface {}
    2. struct MyComponentB : IComponentData, IMyInterface {}
    And I have some Entity array. I need to inject ComponentData from those entities to List<IMyInterface>. But here is no way to get component data by interface.

    I know that I can use GetAssignableComponentTypes(Type) that returned a list of matching types, but then I can't use getComponentData<T>(Entity) cause I can't use Type as T (I also don't understand for what is GetAssignableComponentTypes(Type) exist).

    Also I tried something like this. But MyComponentContainer isn't blittable.
    Code (CSharp):
    1. struct MyComponentA : IComponentData, IMyInterface {}
    2. struct MyComponentB : IComponentData, IMyInterface {}
    3. struct MyComponentContainer {
    4.     IMyInterface content;
    5. }
    I tried to use GetComponentTypes(Entity) and check if it is IMyInterface. But I see no way to get component data through ComponentType

    So is here a way to get ComponentData which matching to interface without manualy iterating through all possible ArchtypeChunks?
     
    Last edited: Dec 8, 2018
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    ECS is designed heavily on generic typing throughout the lib, the most you can utilize the interface is to use `where` constraint. No matter what you do the top call site will have a concrete type, the library was built to enforce this strongly. Better build some kind of EntityArchetypeQuery to represent your "component group" instead of an interface. Use interface for behaviour of that type not for grouping.

    ECS is centered around registering each struct to an integer type index as you use the lib. How would ECS know how many components you have implemented the interface in your project before seeing them? (It is possible https://stackoverflow.com/a/26750/862147 but not in the design of ECS)
     
    Tony_Max and eizenhorn like this.
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Damn! You faster @5argon again :D just kidding as usual :) How your ECS UI implementation living? :)
     
  4. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    353
    For what purpose is GetAssignableComponentTypes(Type) and GetComponentTypes(Entity) exists?
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Both seems to be for "out of ECS" things, it is to bridge ECS typing system to outside of the library. What you want to do is taking the type out, based on an interface, and use them with ECS again. That is not possible by design. But if you write other library to take data from ECS it could be useful. GetComponentTypes is used internally for the debugger which it lists all the types to see.


    I believe it is working 100% now! Missing just documentations and benchmarking before I can put on GitHub..
     
    Deleted User likes this.