# Objective Move `StateScoped` and `log_transitions` to `bevy_state`, since they're useful for end users. Addresses #12852, although not in the way the issue had in mind. ## Solution - Added `bevy_hierarchy` to default features of `bevy_state`. - Move `log_transitions` to `transitions` module. - Move `StateScoped` to `state_scoped` module, gated behind `bevy_hierarchy` feature. - Refreshed implementation. - Added `enable_state_coped_entities<S: States>()` to add required machinery to `App` for clearing state-scoped entities. ## Changelog - Added `log_transitions` for displaying state transitions. - Added `StateScoped` for binding entity lifetime to state and app `enable_state_coped_entities` to register cleaning behavior. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
19 lines
701 B
Rust
19 lines
701 B
Rust
//! Tools for debugging states.
|
|
|
|
use bevy_ecs::event::EventReader;
|
|
use bevy_state::state::{StateTransitionEvent, States};
|
|
use bevy_utils::tracing::info;
|
|
|
|
/// Logs state transitions into console.
|
|
///
|
|
/// This system is provided to make debugging easier by tracking state changes.
|
|
pub fn log_transitions<S: States>(mut transitions: EventReader<StateTransitionEvent<S>>) {
|
|
// State internals can generate at most one event (of type) per frame.
|
|
let Some(transition) = transitions.read().last() else {
|
|
return;
|
|
};
|
|
let name = std::any::type_name::<S>();
|
|
let StateTransitionEvent { exited, entered } = transition;
|
|
info!("{} transition: {:?} => {:?}", name, exited, entered);
|
|
}
|