Search Unity

ComponentType and inheritance

Discussion in 'Entity Component System' started by FlawlessRuby, Oct 19, 2019.

  1. FlawlessRuby

    FlawlessRuby

    Joined:
    Oct 30, 2017
    Posts:
    3
    Heya, folks.

    I'm trying to filter Entities with base class while derived class component is attached to entity.
    Classes:
    Code (CSharp):
    1. public class ObjectCactusStageSystem : ObjectStageSystem
    Code (CSharp):
    1. public class ObjectStageSystem : MonoBehaviour
    Filter:
    Code (CSharp):
    1.  active_group_desc = new EntityQueryDesc
    2.         {
    3.             All = new ComponentType[] { typeof(DropStage), ComponentType.ReadOnly<ObjectStageSystem>() }
    4.         };
    But it doesn't quite work.

    When the base class is attached to entity it works perfectly.
    Am I missing something dramatically? Is it possible to achieve?

    Thanks.
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    ComponentType doesn't take inheritance into account. You have to specify each derived type you're looking for, or check for subtypes per-chunk.

    I think they're working on a solution for this as it's been an issue dealing with Transform -> RectTransform.

    My solution was to have 2 queries, one for Transform, one for RectTransform, and then run Entities.ForEach per-query.

    In my RectTransform foreach function I simply called the base Transform Version of the function.
     
    FlawlessRuby likes this.
  3. FlawlessRuby

    FlawlessRuby

    Joined:
    Oct 30, 2017
    Posts:
    3
    Hi, @recursive.

    Thanks for the info. I'm dealing with it same way as you right now.

    But as the count of derived types is growing system's code is getting quite bulky.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    The queries were designed to be efficient for Components (IComponentData) which can't be derived (as they're structs). However they do support querying classical MonoBehaviours (and other managed types) to provide support for a hybrid solution for existing projects allowing parts to be converted at a time and for engine components that Unity hasn't build native Entity support for yet.

    I'm curious, are you trying to develop a new project only using classic MonoBehaviours but with the Entities package? Or just trying to convert an existing solution to use Systems?
     
  5. FlawlessRuby

    FlawlessRuby

    Joined:
    Oct 30, 2017
    Posts:
    3
    Project contains some moving pure ECS elements. And those elements performer changes on static GameObject elements.

    For example, System queries for all entities bound to GameObject and fires method call in each ObjectStageSystem which results in some classic changes on the GameObject (like child switch on/off, animation triggers).