Add SystemCondition-satisfying closure "resource_exists_and"
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
Bevy has [three similar functions](https://docs.rs/bevy/latest/bevy/?search=resource_exists):
- `resource_exists`
- `resource_exists_and_changed`
- `resource_exists_and_equals`
## What solution would you like?
Add another system `resource_exists_and` which takes a more arbitrary condition, e.g.
```rust
struct CombatQueue>;
update_new_combat_ui.run_if(resource_exists_and(|queue: &CombatQueue| queue.0.is_empty()))
```
## What alternative(s) have you considered?
I suppose there's other ways of doing very similar things (e.g. you could make a new State and have systems condition on them, then make a system in PostUpdate or PreUpdate to update said State depending on the condition of the Resource) but I think this is convenient and reduces the lines of code needed a lot, especially for one-off and simple condition checks where such a State would just be tracking a Resource anyway.
## Additional context
I've implemented this for my personal usage as follows, by copying+modifying the `resource_exists_and_equals` system:
```rust
pub fn resource_exists_and(condition: impl Fn(&T) -> bool) -> impl FnMut(Option>) -> bool
where
T: Resource,
{
move |res: Option>| match res {
Some(res) => condition(&res),
None => false,
}
}
```
It's working well for me so far.
Contributor guide
Research direction
Locate the existing `resource_exists`, `resource_exists_and_changed`, and `resource_exists_and_equals` implementations and their related tests. Add the requested `resource_exists_and` condition with the demonstrated predicate behavior, including returning false when the resource is absent, then run the relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100