Skip to content

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

Closed

Description

@alice-i-cecile

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.

Activity

  1. added
    C-FeatureA new feature, making something new possible
    A-ECSEntities, components, systems, and events
    on Feb 24, 2023
  2. SkiFire13 commented on Feb 24, 2023

    @SkiFire13
    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.

  3. alice-i-cecile commented on Feb 24, 2023

    @alice-i-cecile
    MemberAuthor

    Ah right, that's caused by #7680.

  4. BoxyUwU commented on Feb 24, 2023

    @BoxyUwU
    Member

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

  5. BoxyUwU commented on Feb 24, 2023

    @BoxyUwU
    Member

    sidenote: Or does not return bool it returns ()

  6. added a commit that references this issue on Jun 19, 2023
    f673ee4
  7. added a commit that references this issue on Jan 27, 2024
    6529d2e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions