Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Using SharedComponentFilter on Query constructed using Any

Discussion in 'Entity Component System' started by MichelangeloConserva, Dec 28, 2020.

  1. MichelangeloConserva

    MichelangeloConserva

    Joined:
    Apr 16, 2020
    Posts:
    1
    Hello everyone!

    I got a simple problem with the SharedComponentFilter.

    When I construct a Query with the full list of ComponentType then everything works perfectly, however if I construct the query using "Any" in "EntityQueryDesc" then I get the following error.

    InvalidOperationException: Trying to get iterator for CompShared but the required component type was not declared in the EntityQuery.



    This code works perfectly.
    Code (CSharp):
    1. public struct CompShared : ISharedComponentData
    2. {
    3.     public int Id;
    4.  
    5.     public CompShared(int id)
    6.     {
    7.         Id = id;
    8.     }
    9. }
    10.  
    11. public struct Comp : IComponentData
    12. {
    13.     public int SomeValue;
    14. }
    15.  
    16. public class FilterSystem : SystemBase
    17. {
    18.     private EntityQuery _query;
    19.     protected override void OnCreate()
    20.     {
    21.         _query = GetEntityQuery(typeof(CompShared), ComponentType.ReadOnly<Comp>());
    22.  
    23.         var shared1 = new CompShared()
    24.         {
    25.             Id = 1
    26.         };
    27.         var shared2 = new CompShared()
    28.         {
    29.             Id = 2
    30.         };
    31.         for (int i = 0; i < 10; i++)
    32.         {
    33.             var e = EntityManager.CreateEntity(typeof(CompShared), typeof(Comp));
    34.             if (i < 7)
    35.                 EntityManager.SetSharedComponentData(e, shared1);
    36.             else
    37.                 EntityManager.SetSharedComponentData(e, shared2);
    38.         }
    39.     }
    40.  
    41.     protected override void OnUpdate()
    42.     {
    43.         Debug.Log(_query.CalculateEntityCount());
    44.         _query.SetSharedComponentFilter(new CompShared(2));
    45.         Debug.Log(_query.CalculateEntityCount());
    46.         _query.ResetFilter();
    47.     }
    48. }

    Whereas this one throws the error.
    Code (CSharp):
    1. public struct CompShared : ISharedComponentData
    2. {
    3.     public int Id;
    4.  
    5.     public CompShared(int id)
    6.     {
    7.         Id = id;
    8.     }
    9. }
    10.  
    11. public struct Comp : IComponentData
    12. {
    13.     public int SomeValue;
    14. }
    15.  
    16. public class FilterSystem : SystemBase
    17. {
    18.     private EntityQuery _query;
    19.     protected override void OnCreate()
    20.     {
    21.         _query = GetEntityQuery(new EntityQueryDesc
    22.         {
    23.             Any = new ComponentType[]
    24.             {
    25.                 typeof(CompShared)
    26.             },
    27.             Options = EntityQueryOptions.FilterWriteGroup
    28.            
    29.         });
    30.  
    31.         var shared1 = new CompShared()
    32.         {
    33.             Id = 1
    34.         };
    35.         var shared2 = new CompShared()
    36.         {
    37.             Id = 2
    38.         };
    39.         for (int i = 0; i < 10; i++)
    40.         {
    41.             var e = EntityManager.CreateEntity(typeof(CompShared), typeof(Comp));
    42.             if (i < 7)
    43.                 EntityManager.SetSharedComponentData(e, shared1);
    44.             else
    45.                 EntityManager.SetSharedComponentData(e, shared2);
    46.         }
    47.     }
    48.  
    49.     protected override void OnUpdate()
    50.     {
    51.         Debug.Log(_query.CalculateEntityCount());
    52.         _query.SetSharedComponentFilter(new CompShared(2));
    53.         Debug.Log(_query.CalculateEntityCount());
    54.         _query.ResetFilter();
    55.     }
    56. }

    Is there a way to use "Any" without this error?