|
| 1 | +//! Demonstrates how to add custom schedules that run in Bevy's `Main` schedule, ordered relative to Bevy's built-in |
| 2 | +//! schedules such as `Update` or `Last`. |
| 3 | +
|
| 4 | +use bevy::app::MainScheduleOrder; |
| 5 | +use bevy::ecs::schedule::{ExecutorKind, ScheduleLabel}; |
| 6 | +use bevy::prelude::*; |
| 7 | + |
| 8 | +#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)] |
| 9 | +struct SingleThreadedUpdate; |
| 10 | + |
| 11 | +#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)] |
| 12 | +struct CustomStartup; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + let mut app = App::new(); |
| 16 | + |
| 17 | + // Create a new [`Schedule`]. For demonstration purposes, we configure it to use a single threaded executor so that |
| 18 | + // systems in this schedule are never run in parallel. However, this is not a requirement for custom schedules in |
| 19 | + // general. |
| 20 | + let mut custom_update_schedule = Schedule::new(SingleThreadedUpdate); |
| 21 | + custom_update_schedule.set_executor_kind(ExecutorKind::SingleThreaded); |
| 22 | + |
| 23 | + // Adding the schedule to the app does not automatically run the schedule. This merely registers the schedule so |
| 24 | + // that systems can look it up using the `Schedules` resource. |
| 25 | + app.add_schedule(custom_update_schedule); |
| 26 | + |
| 27 | + // Bevy `App`s have a `main_schedule_label` field that configures which schedule is run by the App's `runner`. |
| 28 | + // By default, this is `Main`. The `Main` schedule is responsible for running Bevy's main schedules such as |
| 29 | + // `Update`, `Startup` or `Last`. |
| 30 | + // |
| 31 | + // We can configure the `Main` schedule to run our custom update schedule relative to the existing ones by modifying |
| 32 | + // the `MainScheduleOrder` resource. |
| 33 | + // |
| 34 | + // Note that we modify `MainScheduleOrder` directly in `main` and not in a startup system. The reason for this is |
| 35 | + // that the `MainScheduleOrder` cannot be modified from systems that are run as part of the `Main` schedule. |
| 36 | + let mut main_schedule_order = app.world.resource_mut::<MainScheduleOrder>(); |
| 37 | + main_schedule_order.insert_after(Update, SingleThreadedUpdate); |
| 38 | + |
| 39 | + // Adding a custom startup schedule works similarly, but needs to use `insert_startup_after` |
| 40 | + // instead of `insert_after`. |
| 41 | + app.add_schedule(Schedule::new(CustomStartup)); |
| 42 | + |
| 43 | + let mut main_schedule_order = app.world.resource_mut::<MainScheduleOrder>(); |
| 44 | + main_schedule_order.insert_startup_after(PreStartup, CustomStartup); |
| 45 | + |
| 46 | + app.add_systems(SingleThreadedUpdate, single_threaded_update_system) |
| 47 | + .add_systems(CustomStartup, custom_startup_system) |
| 48 | + .add_systems(PreStartup, pre_startup_system) |
| 49 | + .add_systems(Startup, startup_system) |
| 50 | + .add_systems(First, first_system) |
| 51 | + .add_systems(Update, update_system) |
| 52 | + .add_systems(Last, last_system) |
| 53 | + .run(); |
| 54 | +} |
| 55 | + |
| 56 | +fn pre_startup_system() { |
| 57 | + println!("Pre Startup"); |
| 58 | +} |
| 59 | + |
| 60 | +fn startup_system() { |
| 61 | + println!("Startup"); |
| 62 | +} |
| 63 | + |
| 64 | +fn custom_startup_system() { |
| 65 | + println!("Custom Startup"); |
| 66 | +} |
| 67 | + |
| 68 | +fn first_system() { |
| 69 | + println!("First"); |
| 70 | +} |
| 71 | + |
| 72 | +fn update_system() { |
| 73 | + println!("Update"); |
| 74 | +} |
| 75 | + |
| 76 | +fn single_threaded_update_system() { |
| 77 | + println!("Single Threaded Update"); |
| 78 | +} |
| 79 | + |
| 80 | +fn last_system() { |
| 81 | + println!("Last"); |
| 82 | +} |
0 commit comments