Search Unity

[Solved] EntityQuery.SetFilter doesn't work?

Discussion in 'Entity Component System' started by xVergilx, Jul 25, 2019.

  1. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    For some reason, I'm getting:
    Code (CSharp):
    1. InvalidOperationException: Trying to get iterator for Components.Units.OwnerControlGroup but the required component type was not declared in the EntityGroup.
    As soon as I try to apply a filter to:
    Code (CSharp):
    1. EntityQuery query = EntityManager.CreateEntityQuery(typeof(OwnerControlGroup),
    2.                                                     ComponentType.ReadOnly<Unit>(),
    3.                                                     ComponentType.ReadOnly<Translation>());
    4. query.SetFilter(new OwnerControlGroup(id));
    I've tried ComponentType.ReadOnly<OwnerControlGroup>, and ReadWrite<OwnerControlGroup> with the same result.

    GetEntityQuery fails the same way.

    OwnerControlGroup is defined as this:
    Code (CSharp):
    1. using Unity.Entities;
    2. namespace Components.Units {
    3.     public struct OwnerControlGroup : ISharedComponentData {
    4.         public int ControlId;
    5.  
    6.         public OwnerControlGroup(int controlId) {
    7.             ControlId = controlId;
    8.         }
    9.     }
    10. }
    11.  
    Units do have a shared component attached to them.

    What am I missing?
     
  2. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Where are you calling this code from? Usually you use GetFilter in OnCreate, and then SetFilter in OnUpdate. If used otherwise I doubt it would cause issues but maybe?

    A complete shot in the dark, but maybe the constructor is throwing things off? Unity creates a default shared component of your type, and maybe the lack of a parameter-less constructor is throwing off this process. I would add one, and if that doesn't work, try removing the constructor. When doing the latter, if you have a lot of new OwnerControlGroup(ID) lines, you could do a Find/Replace and switch them to new OwnerControlGroup { ControlID = ID}.

    Just some ideas with nothing substantial to back them up.
     
    xVergilx likes this.
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I haven't tried GetFilter I'll try that. I was just setting filter directly via SetFilter + SharedData (as in manual).

    Setting it in both OnCreate and OnUpdate fails for the same reason.

    Already tried, same result.

    What I think I should do is to debug where data missmatches. Perhaps its a bug.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    Show full code, some mistakes on your side, cause SetFilter works fine
    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 : JobComponentSystem
    17. {
    18.     private EntityQuery _query;
    19.     protected override void OnCreateManager()
    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 JobHandle OnUpdate(JobHandle deps)
    42.     {
    43.         Debug.Log(_query.CalculateLength());
    44.         _query.SetFilter(new CompShared(2));
    45.         Debug.Log(_query.CalculateLength());
    46.         _query.ResetFilter();
    47.         return deps;
    48.     }
    49. }
    upload_2019-7-26_9-10-38.png
     
    xVergilx likes this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    System:
    Code (CSharp):
    1. using Archetypes;
    2. using Components.UI.SelectionBox;
    3. using Components.Units;
    4. using Data;
    5. using Unity.Collections;
    6. using Unity.Entities;
    7. using Unity.Jobs;
    8. using Unity.Mathematics;
    9. using Unity.Transforms;
    10. using UnityEngine;
    11.  
    12. namespace Systems.UI {
    13.     public class SelectionBoxSystem : JobComponentSystem {
    14.         #region [Fields]
    15.  
    16.         private EntityQuery _boxQuery;
    17.         private EntityQuery _unitQuery;
    18.  
    19.         private float2 _startScreenPos;
    20.         private float2 _endScreenPos;
    21.  
    22.         private static Camera _camera;
    23.         private static Transform _cameraTrm;
    24.  
    25.         private EndSimulationEntityCommandBufferSystem _ecbSystem;
    26.  
    27.         #endregion
    28.  
    29.         protected override void OnCreate() {
    30.             _boxQuery = EntityManager.CreateEntityQuery(ArchetypesUI.SelectionBox); // ComponentType[] { SelectionBox }
    31.             RequireSingletonForUpdate<SelectionBox>();
    32.  
    33.             _unitQuery = GetEntityQuery(ComponentType.ReadOnly<Unit>(),
    34.                                         ComponentType.ReadOnly<Translation>(),
    35.                                         typeof(OwnerControlGroup));
    36.             _unitQuery.SetFilter(new OwnerControlGroup(1)); // Exception here
    37.  
    38.             _ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    39.         }
    40.  
    41.         /// <summary>
    42.         /// Sets a camera to raycast from
    43.         /// </summary>
    44.         public static void SetCamera(Camera camera) {
    45.             _camera = camera;
    46.             _cameraTrm = _camera.transform;
    47.         }
    48.  
    49.         protected override JobHandle OnUpdate(JobHandle inputDeps) {
    50.             SelectionBox box = _boxQuery.GetSingleton<SelectionBox>();
    51.  
    52.             _startScreenPos = box.Start;
    53.             _endScreenPos = box.End;
    54.  
    55.             float2 screenCenter = (_endScreenPos + _startScreenPos) * 0.5f;
    56.  
    57.             Vector3 vCenter = new Vector3(screenCenter.x, screenCenter.y);
    58.             Vector2 size = math.abs(_startScreenPos - _endScreenPos);
    59.  
    60.             // Half extents used instead of size
    61.             ScreenBounds selectionBounds = new ScreenBounds(screenCenter, size / 2f);
    62.  
    63.             return PerformSelectionTest(selectionBounds, inputDeps);
    64.         }
    65.  
    66.         private JobHandle PerformSelectionTest(ScreenBounds selectionBounds, JobHandle inputDeps) {
    67.             float4x4 vp = _camera.projectionMatrix * _cameraTrm.worldToLocalMatrix;
    68.             float2 screen = new float2(Screen.width, Screen.height);
    69.  
    70.             EntityCommandBuffer.Concurrent ecb = _ecbSystem.CreateCommandBuffer()
    71.                                                            .ToConcurrent();
    72.  
    73.             JobHandle job = new SelectionJob {
    74.                                                  Buffer = ecb,
    75.                                                  ViewPort = vp,
    76.                                                  Screen = screen,
    77.                                                  ScreenBounds = selectionBounds
    78.                                              }.Schedule(_unitQuery, inputDeps);
    79.  
    80.             _ecbSystem.AddJobHandleForProducer(job);
    81.  
    82.             return job;
    83.         }
    84.  
    85.         [RequireComponentTag(typeof(Unit))]
    86.         private struct SelectionJob : IJobForEachWithEntity<Translation> {
    87.             [ReadOnly]
    88.             public float4x4 ViewPort;
    89.  
    90.             [ReadOnly]
    91.             public float2 Screen;
    92.  
    93.             [ReadOnly]
    94.             public ScreenBounds ScreenBounds;
    95.  
    96.             public EntityCommandBuffer.Concurrent Buffer;
    97.  
    98.             public void Execute(Entity entity, int index, [ReadOnly] ref Translation pos) {
    99.                 /*Mathy stuff*/
    100.             }
    101.         }
    102.     }
    103. }
    Unit:
    Code (CSharp):
    1. using Unity.Entities;
    2. namespace Components.Units {
    3.     public struct Unit : IComponentData {
    4.     }
    5. }
    6.  
    Code (CSharp):
    1. using Unity.Entities;
    2. namespace Components.Units {
    3.     public struct OwnerControlGroup : ISharedComponentData {
    4.         public int ControlId;
    5.  
    6.         public OwnerControlGroup(int controlId) {
    7.             ControlId = controlId;
    8.         }
    9.     }
    10. }
    11.  
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    Side advices:
    Are you sure error in this system, cause all works fine for me
     
    xVergilx likes this.
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Yes. I guess my potato pc is cursed.
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    You tested it with IDE attached in debug mode and goes through lines step by step?
     
    xVergilx likes this.
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Yes, I've got no idea what's causing this.

    It could be that type is not mapped properly for some reason via TypeManager.

    Just to be clear, I'm using Unity 2019.1.11f1 & Entities p33 / 0.0.12
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Bloody h. You were right. It was pointing to the different query / system.
    How did I managed to miss that?

    Okay, lesson learned, read stacktrace more carefully.