As the number of systems increases in a project, it becomes harder and harder to maintain their execution orders, especially when we have to specify the orders one system at a time. Bevy provides the SystemSet trait that helps us grouping a set of systems. Then we can specify the orders among these system sets.
To do so, we need to have an enum that derives the SystemSet macro. The enum acts as labels of the system sets. Then we use the configure_sets method of App to specify the orders of the system sets. Finally, we use the in_set method of IntoSystemSetConfigs to specify relations between systems and the system sets.
use bevy::{
app::{App, Startup},
ecs::schedule::{IntoSystemConfigs, IntoSystemSetConfigs, SystemSet},
};
#[derive(SystemSet, Debug, Clone, Hash, Eq, PartialEq)]
enum MySet {
A,
B,
}
fn main() {
App::new()
.configure_sets(Startup, (MySet::A, MySet::B).chain())
.add_systems(
Startup,
(
hello_a1.in_set(MySet::A),
hello_a2.in_set(MySet::A),
hello_b1.in_set(MySet::B),
hello_b2.in_set(MySet::B),
),
)
.run();
}
fn hello_a1() {
println!("Hello A1!");
}
fn hello_a2() {
println!("Hello A2!");
}
fn hello_b1() {
println!("Hello B1!");
}
fn hello_b2() {
println!("Hello B2!");
}
In the output, Hello A1!
and Hello A2!
must be printed before Hello B1!
and Hello B2!
.
Yet, the order between Hello A1!
and Hello A2!
(as well as Hello B1!
and Hello B2!
) is uncertain.
To compile the program successfully, any enum deriving SystemSet must also derive Debug, Clone, Hash, Eq, and PartialEq.
By using SystemSet, we can easily swap the order of the system sets.
configure_sets(Startup, (MySet::B, MySet::A).chain())
➡️ Next: Resources - They Are Singleton Structs
📘 Back: Table of contents