schedules
System's need to be scheduled in order to be used. This is done in your
main() function with .add_systems.
App::new().add_systems(Startup, setup).run();
There are several different ScheduleLabels, on first startup these will run in
order:
- PreStartup
- Startup
- PostStartup
Then on updates:
- First
- PreUpdate
- StateTransition
- RunFixedMainLoop
- This will run FixedMain zero to many times, based on how much time has elapsed.
- Update
- PostUpdate
- Last
chain
The .chain() method can be used to ensure certain systems will run in the
exact provided order, rather than in parallel. In this example update_people
will always run before greet_people:
.add_systems(Update, (hello_world, (update_people, greet_people).chain()))
parameters
Commands
fn setup(mut commands: Commands) {
commands.spawn(Person {
name: "kota".to_string(),
});
}
Commands are used to modify the World in arbitrary ways:
- spawning or despawning entities
- inserting components on new or existing entities
- inserting resources
- etc.
spawn
Create a new entity.
Query
fn print_names(person_query: Query<&Person>) {
for person in person_query.iter() {
println!("Name: {}", person.name);
}
}
Enables access to entities and components from a system, without the need to directly access the world.
Filters
Using filters, such as With or Without you can limit your query to only
entities containing specific additional components.
fn people_with_jobs(person_query: Query<&Person, With<Employed>>) {
Mutable
If we want to make any changes to the data we need to make sure our query is mutable; this will have an effect on scheduling:
fn update_people(mut query: Query<&mut Name, With<Person>>) {
Muli-select
Using tuples, each Query type parameter can contain multiple elements:
Query<(&ComponentA, &ComponentB), (With<ComponentC>, Without<ComponentD>)>
fn person_does_job(person_query: Query<(&Person, &Employed)>) {
for (person, employed) in person_query.iter() {
let job_name: &str = match employed.job {
Job::Doctor => "Doctor",
Job::Firefighter => "Fire Fighter",
Job::Lawyer => "Lawyer",
};
println!("{0} is a {1}", person.name, job_name);
}
}