Skip to content

Commit 155ebf7

Browse files
Write real docs for SystemSet (#19538)
# Objective `SystemSet`s are surprisingly rich and nuanced, but are extremely poorly documented. Fixes #19536. ## Solution Explain the basic concept of system sets, how to create them, and give some opinionated advice about their more advanced functionality. ## Follow-up I'd like proper module level docs on system ordering that I can link to here, but they don't exist. Punting to follow-up! --------- Co-authored-by: theotherphil <[email protected]>
1 parent 02fa833 commit 155ebf7

File tree

1 file changed

+87
-1
lines changed
  • crates/bevy_ecs/src/schedule

1 file changed

+87
-1
lines changed

crates/bevy_ecs/src/schedule/set.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,93 @@ define_label!(
6060
);
6161

6262
define_label!(
63-
/// Types that identify logical groups of systems.
63+
/// System sets are tag-like labels that can be used to group systems together.
64+
///
65+
/// This allows you to share configuration (like run conditions) across multiple systems,
66+
/// and order systems or system sets relative to conceptual groups of systems.
67+
/// To control the behavior of a system set as a whole, use [`Schedule::configure_sets`](crate::prelude::Schedule::configure_sets),
68+
/// or the method of the same name on `App`.
69+
///
70+
/// Systems can belong to any number of system sets, reflecting multiple roles or facets that they might have.
71+
/// For example, you may want to annotate a system as "consumes input" and "applies forces",
72+
/// and ensure that your systems are ordered correctly for both of those sets.
73+
///
74+
/// System sets can belong to any number of other system sets,
75+
/// allowing you to create nested hierarchies of system sets to group systems together.
76+
/// Configuration applied to system sets will flow down to their members (including other system sets),
77+
/// allowing you to set and modify the configuration in a single place.
78+
///
79+
/// Systems sets are also useful for exposing a consistent public API for dependencies
80+
/// to hook into across versions of your crate,
81+
/// allowing them to add systems to a specific set, or order relative to that set,
82+
/// without leaking implementation details of the exact systems involved.
83+
///
84+
/// ## Defining new system sets
85+
///
86+
/// To create a new system set, use the `#[derive(SystemSet)]` macro.
87+
/// Unit structs are a good choice for one-off sets.
88+
///
89+
/// ```rust
90+
/// # use bevy_ecs::prelude::*;
91+
///
92+
/// #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
93+
/// struct PhysicsSystems;
94+
/// ```
95+
///
96+
/// When you want to define several related system sets,
97+
/// consider creating an enum system set.
98+
/// Each variant will be treated as a separate system set.
99+
///
100+
/// ```rust
101+
/// # use bevy_ecs::prelude::*;
102+
///
103+
/// #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
104+
/// enum CombatSystems {
105+
/// TargetSelection,
106+
/// DamageCalculation,
107+
/// Cleanup,
108+
/// }
109+
/// ```
110+
///
111+
/// By convention, the listed order of the system set in the enum
112+
/// corresponds to the order in which the systems are run.
113+
/// Ordering must be explicitly added to ensure that this is the case,
114+
/// but following this convention will help avoid confusion.
115+
///
116+
/// ### Adding systems to system sets
117+
///
118+
/// To add systems to a system set, call [`in_set`](crate::prelude::IntoScheduleConfigs::in_set) on the system function
119+
/// while adding it to your app or schedule.
120+
///
121+
/// Like usual, these methods can be chained with other configuration methods like [`before`](crate::prelude::IntoScheduleConfigs::before),
122+
/// or repeated to add systems to multiple sets.
123+
///
124+
/// ```rust
125+
/// use bevy_ecs::prelude::*;
126+
///
127+
/// #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
128+
/// enum CombatSystems {
129+
/// TargetSelection,
130+
/// DamageCalculation,
131+
/// Cleanup,
132+
/// }
133+
///
134+
/// fn target_selection() {}
135+
///
136+
/// fn enemy_damage_calculation() {}
137+
///
138+
/// fn player_damage_calculation() {}
139+
///
140+
/// let mut schedule = Schedule::default();
141+
/// // Configuring the sets to run in order.
142+
/// schedule.configure_sets((CombatSystems::TargetSelection, CombatSystems::DamageCalculation, CombatSystems::Cleanup).chain());
143+
///
144+
/// // Adding a single system to a set.
145+
/// schedule.add_systems(target_selection.in_set(CombatSystems::TargetSelection));
146+
///
147+
/// // Adding multiple systems to a set.
148+
/// schedule.add_systems((player_damage_calculation, enemy_damage_calculation).in_set(CombatSystems::DamageCalculation));
149+
/// ```
64150
#[diagnostic::on_unimplemented(
65151
note = "consider annotating `{Self}` with `#[derive(SystemSet)]`"
66152
)]

0 commit comments

Comments
 (0)