//! Demonstrates how to add custom schedules that run in Bevy's `Main` schedule, ordered relative to Bevy's built-in1//! schedules such as `Update` or `Last`.23use bevy::{4app::MainScheduleOrder,5ecs::schedule::{ExecutorKind, ScheduleLabel},6prelude::*,7};89#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]10struct SingleThreadedUpdate;1112#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]13struct CustomStartup;1415fn main() {16let mut app = App::new();1718// Create a new [`Schedule`]. For demonstration purposes, we configure it to use a single threaded executor so that19// systems in this schedule are never run in parallel. However, this is not a requirement for custom schedules in20// general.21let mut custom_update_schedule = Schedule::new(SingleThreadedUpdate);22custom_update_schedule.set_executor_kind(ExecutorKind::SingleThreaded);2324// Adding the schedule to the app does not automatically run the schedule. This merely registers the schedule so25// that systems can look it up using the `Schedules` resource.26app.add_schedule(custom_update_schedule);2728// Bevy `App`s have a `main_schedule_label` field that configures which schedule is run by the App's `runner`.29// By default, this is `Main`. The `Main` schedule is responsible for running Bevy's main schedules such as30// `Update`, `Startup` or `Last`.31//32// We can configure the `Main` schedule to run our custom update schedule relative to the existing ones by modifying33// the `MainScheduleOrder` resource.34//35// Note that we modify `MainScheduleOrder` directly in `main` and not in a startup system. The reason for this is36// that the `MainScheduleOrder` cannot be modified from systems that are run as part of the `Main` schedule.37let mut main_schedule_order = app.world_mut().resource_mut::<MainScheduleOrder>();38main_schedule_order.insert_after(Update, SingleThreadedUpdate);3940// Adding a custom startup schedule works similarly, but needs to use `insert_startup_after`41// instead of `insert_after`.42app.add_schedule(Schedule::new(CustomStartup));4344let mut main_schedule_order = app.world_mut().resource_mut::<MainScheduleOrder>();45main_schedule_order.insert_startup_after(PreStartup, CustomStartup);4647app.add_systems(SingleThreadedUpdate, single_threaded_update_system)48.add_systems(CustomStartup, custom_startup_system)49.add_systems(PreStartup, pre_startup_system)50.add_systems(Startup, startup_system)51.add_systems(First, first_system)52.add_systems(Update, update_system)53.add_systems(Last, last_system)54.run();55}5657fn pre_startup_system() {58println!("Pre Startup");59}6061fn startup_system() {62println!("Startup");63}6465fn custom_startup_system() {66println!("Custom Startup");67}6869fn first_system() {70println!("First");71}7273fn update_system() {74println!("Update");75}7677fn single_threaded_update_system() {78println!("Single Threaded Update");79}8081fn last_system() {82println!("Last");83}848586