Skip to content

Commit

Permalink
Allow the editing of startup schedules (#10969)
Browse files Browse the repository at this point in the history
# Objective

Fixes #10968 

## Solution

Pull startup schedules from a list of `ScheduleLabel`s in the same way
the update schedules are handled.

---

## Changelog

- Added `MainScheduleOrder::startup_labels` to allow the editing of the
startup schedule order.

## Migration Guide

- Added a new field to `MainScheduleOrder`, `startup_labels`, for
editing the startup schedule order.
  • Loading branch information
ItsDoot authored Dec 14, 2023
1 parent 70a592f commit a4e0a0c
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ pub struct Last;
/// their order.
#[derive(Resource, Debug)]
pub struct MainScheduleOrder {
/// The labels to run for the [`Main`] schedule (in the order they will be run).
/// The labels to run for the main phase of the [`Main`] schedule (in the order they will be run).
pub labels: Vec<InternedScheduleLabel>,
/// The labels to run for the startup phase of the [`Main`] schedule (in the order they will be run).
pub startup_labels: Vec<InternedScheduleLabel>,
}

impl Default for MainScheduleOrder {
Expand All @@ -135,12 +137,13 @@ impl Default for MainScheduleOrder {
PostUpdate.intern(),
Last.intern(),
],
startup_labels: vec![PreStartup.intern(), Startup.intern(), PostStartup.intern()],
}
}
}

impl MainScheduleOrder {
/// Adds the given `schedule` after the `after` schedule
/// Adds the given `schedule` after the `after` schedule in the main list of schedules.
pub fn insert_after(&mut self, after: impl ScheduleLabel, schedule: impl ScheduleLabel) {
let index = self
.labels
Expand All @@ -149,15 +152,31 @@ impl MainScheduleOrder {
.unwrap_or_else(|| panic!("Expected {after:?} to exist"));
self.labels.insert(index + 1, schedule.intern());
}

/// Adds the given `schedule` after the `after` schedule in the list of startup schedules.
pub fn insert_startup_after(
&mut self,
after: impl ScheduleLabel,
schedule: impl ScheduleLabel,
) {
let index = self
.startup_labels
.iter()
.position(|current| (**current).eq(&after))
.unwrap_or_else(|| panic!("Expected {after:?} to exist"));
self.startup_labels.insert(index + 1, schedule.intern());
}
}

impl Main {
/// A system that runs the "main schedule"
pub fn run_main(world: &mut World, mut run_at_least_once: Local<bool>) {
if !*run_at_least_once {
let _ = world.try_run_schedule(PreStartup);
let _ = world.try_run_schedule(Startup);
let _ = world.try_run_schedule(PostStartup);
world.resource_scope(|world, order: Mut<MainScheduleOrder>| {
for &label in &order.startup_labels {
let _ = world.try_run_schedule(label);
}
});
*run_at_least_once = true;
}

Expand Down

0 comments on commit a4e0a0c

Please sign in to comment.