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

Allow same-state transitions and add ChangeState system param #11160

Closed
wants to merge 2 commits into from

Conversation

benfrankel
Copy link
Contributor

@benfrankel benfrankel commented Dec 31, 2023

Objective

Fixes #9130.

Solution

Add a system param ChangeState<S> to set the next state depending on the current state. Remove the same-state check from apply_state_transition.

See #11158 for an alternative approach.


Changelog

  • Added a system param ChangeState<S> to set the next state depending on the current state.
  • Removed the check preventing state transitions from the current state to itself.

Migration Guide

NextState can now trigger state transitions from current_state to itself.

To preserve the old behavior, replace instances of next_state: ResMut<NextState<S>> with state: ChangeState<S>, and replace next_state.set(Foo) with state.change(Foo).

@benfrankel
Copy link
Contributor Author

It might be worth updating examples to use ChangeState instead of ResMut<NextState>, but I'm not sure, so I haven't done that for now.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Dec 31, 2023
@MiniaczQ
Copy link
Contributor

It might be worth updating examples to use ChangeState instead of ResMut<NextState>, but I'm not sure, so I haven't done that for now.

Looking for obscure wrappers is difficult without examples, good idea

@benfrankel
Copy link
Contributor Author

I could also remove ChangeState::set_forced, since the name might be confusing, and you can get the same result with ChangeState::next.set or just NextState::set.

@benfrankel
Copy link
Contributor Author

benfrankel commented Dec 31, 2023

Renamed ChangeState::set to ChangeState::change, removed ChangeState::set_forced, and added ChangeState::reload to explicitly transition from the current state to itself.

Updated examples to use ChangeState instead of ResMut<NextState>, and added a restart on R press feature to the alien_cake_addict example.

@hymm hymm self-requested a review January 1, 2024 22:09
@benfrankel
Copy link
Contributor Author

I had an idea that might make the names more coherent. I won't include it in this PR, but figured I'd mention it anyways:

Rename State -> CurrentState, and ChangeState -> State. Then the relationship between the three types would be more obvious, and the most commonly used / most flexible type would have the shortest name. The downside is that it may obscure the fact that multiple systems taking state: State<S> can't run in parallel (because state.next: ResMut<NextState<S>>).

@MiniaczQ
Copy link
Contributor

ChangeState -> State will be misleading. I think a more honest name would be NextState, but it's not worth the breaking change.

@benfrankel
Copy link
Contributor Author

benfrankel commented May 6, 2024

Closing as outdated after #11426.

The issue in #9130 persists, mentioned in the Future Work section of the computed states PR:

NextState::ReEnter - this would allow you to trigger exit & entry systems for the current state type. We can potentially also add a NextState::ReEnterRecursive to also re-trigger any states that depend on the current one.

Alternatively, a similar approach as in this PR may still be possible -- I haven't read through the computed states PR.

@benfrankel benfrankel closed this May 6, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need the OnEnter() and OnExit() to be run even if when transitioning to and from the same state.
4 participants