Skip to content
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

Add in_any_state and in_all_states run conditions #11221

Closed
ramirezmike opened this issue Jan 5, 2024 · 5 comments
Closed

Add in_any_state and in_all_states run conditions #11221

ramirezmike opened this issue Jan 5, 2024 · 5 comments
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Controversial There is active debate or serious implications around merging this PR

Comments

@ramirezmike
Copy link
Contributor

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

As games get more complex, users may find themselves wanting to setup a chain of systems to run in multiple separate states or on specific variant overlaps of multiple state types. While this is currently possible with combinator operations, it can be verbose and these patterns may be common enough to have dedicated run conditions. Searching for "or_else" on the discord yields quite a few instances of people asking (👀) how to do an OR with states.

What solution would you like?

Add two run conditions for handling multiple states: in_any_state for OR and in_all_states for AND. There's a good example to start from here for the OR version.

What alternative(s) have you considered?

Using or_else or and_then will suffice but again, it can get verbose

.run_if(in_state(AppState::InGame).or_else(in_state(OtherState::PreGame)).or_else(in_state(AppState::TitleScreen)))

versus

.run_if(in_any_state([AppState::InGame, OtherState::PreGame, AppState::TitleScreen]))

Additional context

The "in_any_state" may be a bit more useful than the "in_all_states" as the latter is only useful if you're using multiple types of States. Additionally, depending on changes like #9942 this might get more complicated or be handled differently anyway?

@ramirezmike ramirezmike added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jan 5, 2024
@tim-blackbird tim-blackbird added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed S-Needs-Triage This issue needs to be labelled C-Feature A new feature, making something new possible labels Jan 9, 2024
@alice-i-cecile alice-i-cecile added the X-Controversial There is active debate or serious implications around merging this PR label Jan 15, 2024
@alice-i-cecile
Copy link
Member

There are a number of proposed state changes in flight right now; I think we should land some of them, but we should do so with both eyes open and take a holistic view :)

@lee-orr
Copy link
Contributor

lee-orr commented Feb 2, 2024

@ramirezmike - Do you think #11426 would end up resolving this use case? If that PR is merged, you'd approach this by creating a ComputedState that defines this sequence of and/or releationships, and then you can use it as a single state.

@ramirezmike
Copy link
Contributor Author

@lee-orr - That looks like it'd work well for this use case; I would say if that gets merged then this can be closed.

@benfrankel
Copy link
Contributor

benfrankel commented May 23, 2024

This is also fixed in pyri_state without the need for defining a second state:

add_systems(Update, MyState::with(|x| matches!(x, MyState::X | MyState::Y | ...)).on_update(my_systems));

Or if you want to look at states of different types (or anything in the ECS world), you can write your own run condition.

@alice-i-cecile
Copy link
Member

I think that between the new computed states and a solid easy-to-swap-out third party solution the core need here is resolved.

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-Usability A targeted quality-of-life change that makes Bevy easier to use X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants