Skip to content

Add any_for_filter to built-in run conditions #17326

@Jaso333

Description

@Jaso333

What problem does this solve or what need does it fill?

Currently, the only built-in run condition like this is any_with_component. However, this very quickly loses value on the systems that depend on a combination of components including the provided component type. Compounding any_with_component doesn't address this as it doesn't look at archetypes like a filter does.

Take the following system for example:

fn update(mut enemies: Query<(&EnemyMovement, &mut Transform)>, time: Res<Time>) {
    for (movement, mut transform) in enemies.iter_mut() {
        transform.translation.y -= time.delta_secs() * movement.speed;
    }
}

This is a very basic system, but the built-in run conditions do not provide means to limit the scheduling of this system. If I were to use any_with_component::<EnemyMovement>, this system will still run if there are EnemyMovement components that exist without Transform, and therefore time would be wasted scheduling this system for execution in that frame. Instead, I've used the suggested condition:

app.add_systems(
  Update,
  update.run_if(any_for_filter::<(With<EnemyMovement>, With<Transform>)>),
);

What solution would you like?

I'm using this in every project in order to appropriately limit my scheduled systems:

pub fn any_for_filter<F: QueryFilter>(entities: Query<(), F>) -> bool {
    !entities.is_empty()
}

What alternative(s) have you considered?

No alternatives.

Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions