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

StateTransitionEvent #11089

Merged
merged 11 commits into from
Jan 8, 2024
Merged

Conversation

SIGSTACKFAULT
Copy link
Contributor

@SIGSTACKFAULT SIGSTACKFAULT commented Dec 26, 2023

Objective

  • Make it possible to react to arbitrary state changes
  • this will be useful regardless of the other changes to states currently being discussed

Solution

  • added StateTransitionEvent<S> struct
  • previously, this would have been impossible:
#[derive(States, Eq, PartialEq, Hash, Copy, Clone, Default)]
enum MyState {
  #[default]
  Foo,
  Bar(MySubState),
}

enum MySubState {
  Spam,
  Eggs,
}

app.add_system(Update, on_enter_bar);

fn on_enter_bar(trans: EventReader<StateTransition<MyState>>){
  for (befoare, after) in trans.read() {
    match before, after {
      MyState::Foo, MyState::Bar(_) => info!("detected transition foo => bar");
      _, _ => ();
    }
  }
}

Changelog

  • Added
    • StateTransitionEvent<S> - Fired on state changes of S

Migration Guide

N/A no breaking changes

@SIGSTACKFAULT SIGSTACKFAULT changed the title StateTransitionEvent event StateTransitionEvent Dec 26, 2023
Copy link
Contributor

@Nilirad Nilirad left a comment

Choose a reason for hiding this comment

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

Interesting move. Just a couple of terminology nits.

crates/bevy_ecs/src/schedule/state.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/schedule/state.rs Outdated Show resolved Hide resolved
@Nilirad Nilirad added A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use labels Dec 26, 2023
Co-authored-by: Federico Rinaldi <gisquerin@gmail.com>
@SIGSTACKFAULT
Copy link
Contributor Author

just making sure y'all aren't waiting for me to do something

Copy link
Contributor

@Nilirad Nilirad left a comment

Choose a reason for hiding this comment

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

Looks almost right. Just a small doc nit I forgot. For the rest it's ok.

crates/bevy_ecs/src/schedule/state.rs Outdated Show resolved Hide resolved
@Nilirad Nilirad added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 2, 2024
Co-authored-by: Federico Rinaldi <gisquerin@gmail.com>
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 8, 2024
Merged via the queue into bevyengine:main with commit 1260b7b Jan 8, 2024
22 checks passed
@jannik4
Copy link
Contributor

jannik4 commented Feb 2, 2024

I know I'm a bit late on this, but @SIGSTACKFAULT could you maybe give #10334 a look. I think it also does cover your use case and in addition to that it allows to react to arbitrary state changes at the "right" time (in between enter/exit).

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 simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants