Skip to content

Commit 17a7744

Browse files
Add error message if states schedule missing (usually because StatesPlugin hasn't been added) (#14160)
# Objective - Helps improve #14151 ## Solution - At least return an error message from the `Option::unwrap()` call when we try to access the `StateTransition` schedule --------- Co-authored-by: Martín Maita <47983254+mnmaita@users.noreply.github.com>
1 parent 99c9218 commit 17a7744

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

crates/bevy_state/src/app.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ impl AppExtStates for SubApp {
7373
self.init_resource::<State<S>>()
7474
.init_resource::<NextState<S>>()
7575
.add_event::<StateTransitionEvent<S>>();
76-
let schedule = self.get_schedule_mut(StateTransition).unwrap();
76+
let schedule = self.get_schedule_mut(StateTransition).expect(
77+
"The `StateTransition` schedule is missing. Did you forget to add StatesPlugin or DefaultPlugins before calling init_state?"
78+
);
7779
S::register_state(schedule);
7880
let state = self.world().resource::<State<S>>().get().clone();
7981
self.world_mut().send_event(StateTransitionEvent {
@@ -94,7 +96,9 @@ impl AppExtStates for SubApp {
9496
self.insert_resource::<State<S>>(State::new(state.clone()))
9597
.init_resource::<NextState<S>>()
9698
.add_event::<StateTransitionEvent<S>>();
97-
let schedule = self.get_schedule_mut(StateTransition).unwrap();
99+
let schedule = self.get_schedule_mut(StateTransition).expect(
100+
"The `StateTransition` schedule is missing. Did you forget to add StatesPlugin or DefaultPlugins before calling init_state?"
101+
);
98102
S::register_state(schedule);
99103
self.world_mut().send_event(StateTransitionEvent {
100104
exited: None,

0 commit comments

Comments
 (0)