bevyengine / bevyengine/bevy

Query filter for component value rather than component existence

Open
#5,639 5 comments 3 reactions 0 assignees View on GitHub
A-ECS C-Feature D-Complex X-Needs-SME
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

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

I'm working on a game that uses a lot of different 'status effects'. These status effects are presently implemented on each character as an inserted/removed component, but I am interested in exploring these with always-inserted bool fields instead (akin to `Visibility`). The main issue I have is that I won't be able to filter out entities as easily as I can presently (like finding all poisoned entities by using `With`).

## What solution would you like?

**UPDATE**: Working proof of concept: https://crates.io/crates/bevy_mod_check_filter

A Query Filter pair akin to `With`/`Without`, that instead of checking for component existence, checks for component _value_. Given we don't have full const generics yet, this should probably be limited to `true`/`false` for simplicity. Alternatively, the current value could be compared to the `Default` value.

The name for these filters? Unsure. Maybe `Truthy`/`Falsey` or `Enabled`/`Disabled`?

### Illustration

Today:
```rust
#[derive(Component)]
struct Poisoned;
fn all_poisoned(entities: Query<&Name, With>) {
// ...
}
```

New query filter (see how we don't lose the ergonomics of marker components?):
```rust
#[derive(Component)]
struct Poisoned(bool);
// We will need some machinery like Deref or Into to extract the current value, but I'll leave that for discussion.
fn all_poisoned(entities: Query<&Name, Enabled>) {
}
```

## What alternative(s) have you considered?

Filtering the way that the `Visibility` struct does presently. It's not the best ergonomics, though.

## Additional context

Related to the discussion found in #3796.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.