Search Unity

Multiple archetypes in one system?

Discussion in 'Entity Component System' started by justaguygames, Jul 16, 2019.

  1. justaguygames

    justaguygames

    Joined:
    Mar 4, 2015
    Posts:
    21
    I'm pretty new to ECS and most of the example systems I see work on one type of entity defined by a single archetype. Is it bad practice for a system to work with two distinct sets of entities.

    What functions cover this and is it normal behaviour? (Maybe code examples I've seen are a little dated)

    Thanks
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    No, it's totally fine and very common.

    Just make sure the scope of your system doesn't expand beyond where it belongs.
     
  3. justaguygames

    justaguygames

    Joined:
    Mar 4, 2015
    Posts:
    21
    Aye thanks. I see that as the danger, if you let it get out of hand. Are there any official examples I can reference that use two multiple groups in a system?
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Not sure about specific examples, but you can always look at the source of systems that Unity has developed. For example the ParentSystem uses 4 queries

    Code (CSharp):
    1. protected override void OnCreate()
    2.         {
    3.             m_NewParentsGroup = GetEntityQuery(new EntityQueryDesc
    4.             {
    5.                 All = new ComponentType[]
    6.                 {
    7.                     ComponentType.ReadOnly<Parent>(),
    8.                     ComponentType.ReadOnly<LocalToWorld>(),
    9.                     ComponentType.ReadOnly<LocalToParent>()
    10.                 },
    11.                 None = new ComponentType[]
    12.                 {
    13.                     typeof(PreviousParent)
    14.                 },
    15.                 Options = EntityQueryOptions.FilterWriteGroup
    16.             });
    17.             m_RemovedParentsGroup = GetEntityQuery(new EntityQueryDesc
    18.             {
    19.                 All = new ComponentType[]
    20.                 {
    21.                     typeof(PreviousParent)
    22.                 },
    23.                 None = new ComponentType[]
    24.                 {
    25.                     typeof(Parent)
    26.                 },
    27.                 Options = EntityQueryOptions.FilterWriteGroup
    28.             });
    29.             m_ExistingParentsGroup = GetEntityQuery(new EntityQueryDesc
    30.             {
    31.                 All = new ComponentType[]
    32.                 {
    33.                     ComponentType.ReadOnly<Parent>(),
    34.                     ComponentType.ReadOnly<LocalToWorld>(),
    35.                     ComponentType.ReadOnly<LocalToParent>(),
    36.                     typeof(PreviousParent)
    37.                 },
    38.                 Options = EntityQueryOptions.FilterWriteGroup
    39.             });
    40.             m_DeletedParentsGroup = GetEntityQuery(new EntityQueryDesc
    41.             {
    42.                 All = new ComponentType[]
    43.                 {
    44.                     typeof(Child)
    45.                 },
    46.                 None = new ComponentType[]
    47.                 {
    48.                     typeof(LocalToWorld)
    49.                 },
    50.                 Options = EntityQueryOptions.FilterWriteGroup
    51.             });
    52.         }