Skip to content

Commit

Permalink
Move schedule name into Schedule (bevyengine#9600)
Browse files Browse the repository at this point in the history
# Objective

- Move schedule name into `Schedule` to allow the schedule name to be
used for errors and tracing in Schedule methods
- Fixes bevyengine#9510

## Solution

- Move label onto `Schedule` and adjust api's on `World` and `Schedule`
to not pass explicit label where it makes sense to.
- add name to errors and tracing.
- `Schedule::new` now takes a label so either add the label or use
`Schedule::default` which uses a default label. `default` is mostly used
in doc examples and tests.

---

## Changelog

- move label onto `Schedule` to improve error message and logging for
schedules.

## Migration Guide

`Schedule::new` and `App::add_schedule`
```rust
// old
let schedule = Schedule::new();
app.add_schedule(MyLabel, schedule);

// new
let schedule = Schedule::new(MyLabel);
app.add_schedule(schedule);
```

if you aren't using a label and are using the schedule struct directly
you can use the default constructor.
```rust
// old
let schedule = Schedule::new();
schedule.run(world);

// new
let schedule = Schedule::default();
schedule.run(world);
```

`Schedules:insert`
```rust
// old
let schedule = Schedule::new();
schedules.insert(MyLabel, schedule);

// new
let schedule = Schedule::new(MyLabel);
schedules.insert(schedule);
```

`World::add_schedule`
```rust
// old
let schedule = Schedule::new();
world.add_schedule(MyLabel, schedule);

// new
let schedule = Schedule::new(MyLabel);
world.add_schedule(schedule);
```
  • Loading branch information
hymm authored Aug 28, 2023
1 parent c440de0 commit 33fdc5f
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 116 deletions.
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/components/archetype_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct A<const N: u16>(f32);
fn setup(system_count: usize) -> (World, Schedule) {
let mut world = World::new();
fn empty() {}
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
for _ in 0..system_count {
schedule.add_systems(empty);
}
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/empty_archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn par_for_each(

fn setup(parallel: bool, setup: impl FnOnce(&mut Schedule)) -> (World, Schedule) {
let mut world = World::new();
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
if parallel {
world.insert_resource(ComputeTaskPool(TaskPool::default()));
}
Expand Down
8 changes: 4 additions & 4 deletions benches/benches/bevy_ecs/scheduling/run_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn run_condition_yes(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
for amount in 0..21 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems(empty.run_if(yes));
for _ in 0..amount {
schedule.add_systems((empty, empty, empty, empty, empty).distributive_run_if(yes));
Expand All @@ -41,7 +41,7 @@ pub fn run_condition_no(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
for amount in 0..21 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems(empty.run_if(no));
for _ in 0..amount {
schedule.add_systems((empty, empty, empty, empty, empty).distributive_run_if(no));
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn run_condition_yes_with_query(criterion: &mut Criterion) {
query.single().0
}
for amount in 0..21 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems(empty.run_if(yes_with_query));
for _ in 0..amount {
schedule.add_systems(
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn run_condition_yes_with_resource(criterion: &mut Criterion) {
res.0
}
for amount in 0..21 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems(empty.run_if(yes_with_resource));
for _ in 0..amount {
schedule.add_systems(
Expand Down
8 changes: 4 additions & 4 deletions benches/benches/bevy_ecs/scheduling/running_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn empty_systems(criterion: &mut Criterion) {
group.measurement_time(std::time::Duration::from_secs(3));
fn empty() {}
for amount in 0..5 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
for _ in 0..amount {
schedule.add_systems(empty);
}
Expand All @@ -33,7 +33,7 @@ pub fn empty_systems(criterion: &mut Criterion) {
});
}
for amount in 1..21 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
for _ in 0..amount {
schedule.add_systems((empty, empty, empty, empty, empty));
}
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn busy_systems(criterion: &mut Criterion) {
world.spawn_batch((0..ENTITY_BUNCH).map(|_| (A(0.0), B(0.0), C(0.0), D(0.0))));
world.spawn_batch((0..ENTITY_BUNCH).map(|_| (A(0.0), B(0.0), C(0.0), E(0.0))));
for system_amount in 0..5 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems((ab, cd, ce));
for _ in 0..system_amount {
schedule.add_systems((ab, cd, ce));
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn contrived(criterion: &mut Criterion) {
world.spawn_batch((0..ENTITY_BUNCH).map(|_| (A(0.0), B(0.0))));
world.spawn_batch((0..ENTITY_BUNCH).map(|_| (C(0.0), D(0.0))));
for system_amount in 0..5 {
let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems((s_0, s_1, s_2));
for _ in 0..system_amount {
schedule.add_systems((s_0, s_1, s_2));
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/scheduling/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn schedule(c: &mut Criterion) {

world.spawn_batch((0..10000).map(|_| (A(0.0), B(0.0), C(0.0), E(0.0))));

let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems((ab, cd, ce));
schedule.run(&mut world);

Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ impl App {
if let Some(schedule) = schedules.get_mut(&schedule) {
schedule.add_systems(systems);
} else {
let mut new_schedule = Schedule::new();
let mut new_schedule = Schedule::new(schedule);
new_schedule.add_systems(systems);
schedules.insert(schedule, new_schedule);
schedules.insert(new_schedule);
}

self
Expand All @@ -403,9 +403,9 @@ impl App {
if let Some(schedule) = schedules.get_mut(&schedule) {
schedule.configure_set(set);
} else {
let mut new_schedule = Schedule::new();
let mut new_schedule = Schedule::new(schedule);
new_schedule.configure_set(set);
schedules.insert(schedule, new_schedule);
schedules.insert(new_schedule);
}
self
}
Expand All @@ -421,9 +421,9 @@ impl App {
if let Some(schedule) = schedules.get_mut(&schedule) {
schedule.configure_sets(sets);
} else {
let mut new_schedule = Schedule::new();
let mut new_schedule = Schedule::new(schedule);
new_schedule.configure_sets(sets);
schedules.insert(schedule, new_schedule);
schedules.insert(new_schedule);
}
self
}
Expand Down Expand Up @@ -798,9 +798,9 @@ impl App {
/// # Warning
/// This method will overwrite any existing schedule at that label.
/// To avoid this behavior, use the `init_schedule` method instead.
pub fn add_schedule(&mut self, label: impl ScheduleLabel, schedule: Schedule) -> &mut Self {
pub fn add_schedule(&mut self, schedule: Schedule) -> &mut Self {
let mut schedules = self.world.resource_mut::<Schedules>();
schedules.insert(label, schedule);
schedules.insert(schedule);

self
}
Expand All @@ -811,7 +811,7 @@ impl App {
pub fn init_schedule(&mut self, label: impl ScheduleLabel) -> &mut Self {
let mut schedules = self.world.resource_mut::<Schedules>();
if !schedules.contains(&label) {
schedules.insert(label, Schedule::new());
schedules.insert(Schedule::new(label));
}
self
}
Expand Down Expand Up @@ -841,7 +841,7 @@ impl App {
let mut schedules = self.world.resource_mut::<Schedules>();

if schedules.get(&label).is_none() {
schedules.insert(label.dyn_clone(), Schedule::new());
schedules.insert(Schedule::new(label.dyn_clone()));
}

let schedule = schedules.get_mut(&label).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ pub struct MainSchedulePlugin;
impl Plugin for MainSchedulePlugin {
fn build(&self, app: &mut App) {
// simple "facilitator" schedules benefit from simpler single threaded scheduling
let mut main_schedule = Schedule::new();
let mut main_schedule = Schedule::new(Main);
main_schedule.set_executor_kind(ExecutorKind::SingleThreaded);
let mut fixed_update_loop_schedule = Schedule::new();
let mut fixed_update_loop_schedule = Schedule::new(RunFixedUpdateLoop);
fixed_update_loop_schedule.set_executor_kind(ExecutorKind::SingleThreaded);

app.add_schedule(Main, main_schedule)
.add_schedule(RunFixedUpdateLoop, fixed_update_loop_schedule)
app.add_schedule(main_schedule)
.add_schedule(fixed_update_loop_schedule)
.init_resource::<MainScheduleOrder>()
.add_systems(Main, Main::run_main);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub trait DetectChangesMut: DetectChanges {
/// # score_changed.initialize(&mut world);
/// # score_changed.run((), &mut world);
/// #
/// # let mut schedule = Schedule::new();
/// # let mut schedule = Schedule::default();
/// # schedule.add_systems(reset_score);
/// #
/// # // first time `reset_score` runs, the score is changed.
Expand Down Expand Up @@ -214,7 +214,7 @@ pub trait DetectChangesMut: DetectChanges {
/// # score_changed_event.initialize(&mut world);
/// # score_changed_event.run((), &mut world);
/// #
/// # let mut schedule = Schedule::new();
/// # let mut schedule = Schedule::default();
/// # schedule.add_systems(reset_score);
/// #
/// # // first time `reset_score` runs, the score is changed.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ mod tests {
world.send_event(TestEvent { i: 3 });
world.send_event(TestEvent { i: 4 });

let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems(|mut events: EventReader<TestEvent>| {
let mut iter = events.iter();

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ mod tests {
}
}

let mut schedule = Schedule::new();
let mut schedule = Schedule::default();
schedule.add_systems((propagate_system, modify_system).chain());
schedule.run(&mut world);
world.clear_trackers();
Expand Down
Loading

0 comments on commit 33fdc5f

Please sign in to comment.