Confusing behaviour with GetEntityQuery(...)

Hello everyone!

Simplified version of my code is

protected override void OnCreate()
{
   _openPopups = GetEntityQuery(typeof(Popup), typeof(IsOpen));
  
   _newPopups = GetEntityQuery(typeof(Popup), typeof(IsOpen));
   _newPopups.AddChangedVersionFilter(typeof(IsOpen));
}

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?

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.

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.

2 Likes

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.

1 Like