Skip to content

Add "resource_exists_and" (#23259)#23260

Open
Satellile wants to merge 9 commits into
bevyengine:mainfrom
Satellile:main
Open

Add "resource_exists_and" (#23259)#23260
Satellile wants to merge 9 commits into
bevyengine:mainfrom
Satellile:main

Conversation

@Satellile
Copy link
Copy Markdown
Contributor

@Satellile Satellile commented Mar 8, 2026

Adds feature: SystemCondition-satisfying closure resource_exists_and.
#23259

Testing

  • I've tested this in my own game on Bevy 0.17, with some simple cases. Haven't noticed any issues.

@kfc35 kfc35 added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 8, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Mar 8, 2026
@mockersf
Copy link
Copy Markdown
Member

mockersf commented Mar 8, 2026

The doc example is a bit hard to read, with a counter being negative and never incremented, and not really showing the system didn't run case: it's present in the example, but not being asserted that it doesn't do anything.

I think I would prefer an example with two resources:

  • a counter just being incremented by the system
  • a resource ShouldRun(bool)

then:

  • add the `Counter(0) resource, run, assert the counter is still 0
  • add the ShouldRun(true) resource, run, assert the counter is 1
  • change the resource to ShouldRun(false), run, assert the counter is still 1

@chescock
Copy link
Copy Markdown
Contributor

chescock commented Mar 9, 2026

I believe this can already be done today using If<Res>. If will cause a system to skip instead of failing if the resource does not exist, and skipping a run condition will skip its system.

// Instead of
my_system.run_if(resource_exists_and(|counter: &Counter| counter.0.is_negative()))
// I think you can use
my_system.run_if(|counter: If<Res<Counter>>| counter.0.is_negative())

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. It's a bit niche, but given that the siblings exist this is fine.

@kfc35 kfc35 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 21, 2026
@mockersf mockersf added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels May 21, 2026
Copy link
Copy Markdown
Member

@mockersf mockersf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc example should be improved

@Satellile
Copy link
Copy Markdown
Contributor Author

Just updated the docs as recommended. However I changed ShouldRun(bool) to ShouldRun(String) and had the run condition be .is_ascii(), as I felt the example should be something that couldn't also be done by resource_exists_and_equals.

@Satellile Satellile requested a review from mockersf May 22, 2026 03:37
@kfc35 kfc35 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels May 22, 2026
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-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

6 participants