docs/bevy/examples/ecs/custom_schedule.rs 3 K · 84 lines · raw · history

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