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

Add .empty() convenience function to Query #2270

Closed
Sheepyhead opened this issue May 28, 2021 · 3 comments
Closed

Add .empty() convenience function to Query #2270

Sheepyhead opened this issue May 28, 2021 · 3 comments
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@Sheepyhead
Copy link
Contributor

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

I find myself occasionally want to check on game state in the way that I want to know if any entities of a given query exist already, but I don't need to use the actual query data. For instance whether the I button opens or closes the inventory window in my game depends on whether the inventory window is already open, and this is most directly checked by finding the marker component for the inventory window.

What solution would you like?

The quick solution would be simply a .empty() function on Query that returns true if the query matches no entities and false otherwise. More ideally I'd also like to be able to make a Query<(), (With<A>, Without<B>, Changed<C>)> so that no data is even passed to the function other than whether the query matches anything or not (having number of matches may be useful but I have no use cases for that currently)

What alternative(s) have you considered?

Currently I write my query like Query<Entity, (With<A>, Without<B>, Changed<C>)> and check on matches using query.iter().count() == 0 which is both wordy and not immediatly explicit. query.empty() would be much nicer

@Sheepyhead Sheepyhead added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels May 28, 2021
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events D-Trivial Nice and easy! A great choice to get started with Bevy and removed S-Needs-Triage This issue needs to be labelled labels May 28, 2021
@alice-i-cecile
Copy link
Member

For those interested in tackling this: adding a .empty() method should be nearly trivial.

Overriding what () returns is likely to be somewhat harder; I expect this is actually blocked on #1843 and #2254 but should be very easier after that.

@NathanSWard
Copy link
Contributor

Can we split apart his issues into two issues?

  1. Add .is_empty() to queries
  2. support Query<(), ...> syntax

@alice-i-cecile
Copy link
Member

@BoxyUwU says querying for () should just work already. Test this out for us?

@bors bors bot closed this as completed in 19db1e4 Jun 2, 2021
ostwilkens pushed a commit to ostwilkens/bevy that referenced this issue Jul 27, 2021
## Problem
- The `Query` struct does not provide an easy way to check if it is empty. 
- Specifically, users have to use `.iter().peekable()` or `.iter().next().is_none()` which is not very ergonomic. 
- Fixes: bevyengine#2270 

## Solution
- Implement an `is_empty` function for queries to more easily check if the query is empty.
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-Feature A new feature, making something new possible D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants