Search Unity

Feedback Confusing behaviour with GetEntityQuery(...)

Discussion in 'Entity Component System' started by SergeyRomanko, Jul 29, 2020.

  1. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    Hello everyone!

    Simplified version of my code is
    Code (CSharp):
    1. protected override void OnCreate()
    2. {
    3.    _openPopups = GetEntityQuery(typeof(Popup), typeof(IsOpen));
    4.  
    5.    _newPopups = GetEntityQuery(typeof(Popup), typeof(IsOpen));
    6.    _newPopups.AddChangedVersionFilter(typeof(IsOpen));
    7. }
    I have two queries with the same set of components. Only one query will be registerd, it is an optimization, it is fine. But what about the filter, it will be added to the single internal query. So my code says that there is no filter on _openPopups but in reality there is a filter on _openPopups because it was applied to seemingly another query.

    When I have ten queries in a system such errors become very hard to find.

    Can we have some error of warning for such situations?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    How do you propose they add errors for this when this behavior is intended 99% of the time.
    If you have 2 jobs back to back wanting to work on the exact same entities/layout, they should cache to the same query.
     
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    I fell into the same “trap”, mostly because I interpreted the “Get” as “get me a new query”. Now that I know its counterpart
    CreateEntityQuery
    , it’s all fine since now it is clear which method returns a new query, and which potentially a cached one.

    Usually, I want the latter,
    CreateEntityQuery
    .

    Edit: It would be helpful if the method was called
    GetOrCreateEntityQuery
    , analog to the
    GetOrCreateSystem
    method.
     
    Last edited: Jul 30, 2020
    Sarkahn and yondercode like this.
  4. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Just spent the past couple of days trying to debug this - thank you! On top of the rename it would be nice to also have a "CreateEntityQuery" function in SystemBase just to make it a bit more consistent.
     
    florianhanke likes this.