Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Filters #834

Merged
merged 1 commit into from
Nov 11, 2020
Merged

Query Filters #834

merged 1 commit into from
Nov 11, 2020

Conversation

cart
Copy link
Member

@cart cart commented Nov 10, 2020

This breaks out a new QueryFilter type from HecsQuery. HecsQueries like Changed<T>, With<T>, and Without<T> have become QueryFilters. The new Query interface now looks like this: Query<Q: HecsQuery, F: QueryFilter = ()>

Ex:

Query<&A> -> Query<&A> (no change)
Query<With<X, With<Y, (&A, &mut B)>>> -> Query<(&A, &mut B), (With<X>, With<Y>)>
Query<Changed<A>> -> Query<&A, Changed<A>>

All direct world query functions now have "filtered" variants. Ex: world.query<Q: HecsQuery> and world.query_filtered<Q: HecsQuery, F: QueryFilter> variants.

This resolves a number of issues:

  • Queries like With<X, With<Y, (&A, &mut B)>> are hard to visually parse
  • It is currently a bit unclear what components will be returned by a query
  • You can't filter "change state" without also grabbing the component
  • There is no good way to get a mutable reference to a Changed (it currently returns a read-only reference). Note that there is a pr out (Make Changed, Mutated and Added work on Queries #625) to resolve this by turning Changed into Changed<&T> and Changed<&mut T>, which we would probably merge if we dont go with this pr.
  • Each new "component filter" requires a new "smart pointer" type

This is how a "real" system changes:

Before

pub fn flex_node_system(
    windows: Res<Windows>,
    mut flex_surface: ResMut<FlexSurface>,
    root_node_query: Query<With<Node, Without<Parent, Entity>>>,
    node_query: Query<With<Node, (Entity, Changed<Style>, Option<&CalculatedSize>)>>,
    changed_size_query: Query<With<Node, (Entity, &Style, Changed<CalculatedSize>)>>,
    children_query: Query<With<Node, (Entity, Changed<Children>)>>,
    mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
) {
}

After

pub fn flex_node_system(
    windows: Res<Windows>,
    mut flex_surface: ResMut<FlexSurface>,
    root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
    node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With<Node>, Changed<Style>)>,
    changed_size_query: Query<
        (Entity, &Style, &CalculatedSize),
        (With<Node>, Changed<CalculatedSize>),
    >,
    children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
    mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
) {
}

@cart cart added C-Enhancement A new feature A-ECS Entities, components, systems, and events labels Nov 10, 2020
@cart cart changed the title query filters Query Filters Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Enhancement A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant