Skip to content

Add Has<C> WorldQuery type to replace Option<&C> when checking existence #7811

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

Closed
alice-i-cecile opened this issue Feb 24, 2023 · 4 comments · Fixed by #8844
Closed

Add Has<C> WorldQuery type to replace Option<&C> when checking existence #7811

alice-i-cecile opened this issue Feb 24, 2023 · 4 comments · Fixed by #8844
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible

Comments

@alice-i-cecile
Copy link
Member

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

I commonly find myself reaching for Option<&C> when I just need to check whether or not a component exists, and then branching on Some versus None.

This works well enough, but:

  1. The access is more restrictive than it needs to be: we request read access, but only actually need existence checking. This limits parallelism, as these systems could run in parallel with mutating systems.
  2. It's less clear to read: the pattern is a bit strange and implicit.
  3. It may be measurably slower.

What solution would you like?

Add a Has<C: Component> type. Implement WorldQuery for this, with no access required.

The item type of each query element should be bool.

What alternative(s) have you considered?

We could do nothing, the eixisting pattern isn't terrible.

We could name this Is<C>: I'm not sure which one is clearer.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events labels Feb 24, 2023
@SkiFire13
Copy link
Contributor

It's pretty horrible but Or<(With<T>,)> (when used as query rather than filter) does yield a bool indicating whether the T is present or not. This is kinda weird though since With<T> by itself does not do this.

@alice-i-cecile
Copy link
Member Author

Ah right, that's caused by #7680.

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 24, 2023

Option<With<T>> works which is nicer than Or<(With<T>,) imo

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 24, 2023

sidenote: Or does not return bool it returns ()

james7132 pushed a commit to james7132/bevy that referenced this issue Jun 19, 2023
# Objective

- Fixes bevyengine#7811 

## Solution

- I added `Has<T>` (and `HasFetch<T>` ) and implemented `WorldQuery`,
`ReadonlyWorldQuery`, and `ArchetypeFilter` it
- I also added documentation with an example and a unit test


I believe I've done everything right but this is my first contribution
and I'm not an ECS expert so someone who is should probably check my
implementation. I based it on what `Or<With<T>,>`, would do. The only
difference is that `Has` does not update component access - adding `Has`
to a query should never affect whether or not it is disjoint with
another query *I think*.

---

## Changelog

## Added
- Added `Has<T>` WorldQuery to find out whether or not an entity has a
particular component.

---------

Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: JoJoJet <[email protected]>
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants