Add error message if states schedule missing (usually because StatesPlugin hasn't been added) (#14160)

# Objective

- Helps improve https://github.com/bevyengine/bevy/issues/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>
This commit is contained in:
Periwink 2024-07-14 11:56:16 -04:00 committed by GitHub
parent 99c9218b56
commit 17a77445e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -73,7 +73,9 @@ impl AppExtStates for SubApp {
self.init_resource::<State<S>>()
.init_resource::<NextState<S>>()
.add_event::<StateTransitionEvent<S>>();
let schedule = self.get_schedule_mut(StateTransition).unwrap();
let schedule = self.get_schedule_mut(StateTransition).expect(
"The `StateTransition` schedule is missing. Did you forget to add StatesPlugin or DefaultPlugins before calling init_state?"
);
S::register_state(schedule);
let state = self.world().resource::<State<S>>().get().clone();
self.world_mut().send_event(StateTransitionEvent {
@ -94,7 +96,9 @@ impl AppExtStates for SubApp {
self.insert_resource::<State<S>>(State::new(state.clone()))
.init_resource::<NextState<S>>()
.add_event::<StateTransitionEvent<S>>();
let schedule = self.get_schedule_mut(StateTransition).unwrap();
let schedule = self.get_schedule_mut(StateTransition).expect(
"The `StateTransition` schedule is missing. Did you forget to add StatesPlugin or DefaultPlugins before calling init_state?"
);
S::register_state(schedule);
self.world_mut().send_event(StateTransitionEvent {
exited: None,