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

Initialize empty schedules when calling .in_schedule if they do not already exist #7911

Merged
merged 8 commits into from
Mar 9, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ impl App {
if let Some(schedule) = schedules.get_mut(&*schedule_label) {
schedule.add_system(system);
} else {
panic!("Schedule {schedule_label:?} does not exist.")
let mut schedule = Schedule::new();
schedule.add_system(system);
schedules.insert(schedule_label, schedule);
}
} else if let Some(default_schedule) = schedules.get_mut(&*self.default_schedule_label) {
default_schedule.add_system(system);
Expand Down Expand Up @@ -1014,7 +1016,12 @@ pub struct AppExit;

#[cfg(test)]
mod tests {
use crate::{App, Plugin};
use bevy_ecs::{
schedule::{OnEnter, States},
system::Commands,
};

use crate::{App, IntoSystemAppConfig, Plugin};

struct PluginA;
impl Plugin for PluginA {
Expand Down Expand Up @@ -1068,4 +1075,23 @@ mod tests {
}
App::new().add_plugin(PluginRun);
}

#[derive(States, PartialEq, Eq, Debug, Default, Hash, Clone)]
enum AppState {
#[default]
MainMenu,
}
fn on_enter(mut commands: Commands) {
commands.spawn_empty();
}

#[test]
fn add_system_should_create_schedule_if_it_does_not_exist() {
let mut app = App::new();
app.add_system(on_enter.in_schedule(OnEnter(AppState::MainMenu)))
.add_state::<AppState>();

app.world.run_schedule(OnEnter(AppState::MainMenu));
assert_eq!(app.world.entities().len(), 1);
}
}