How to query two components simultaneously? #6435
-
The tutorial shows how to query one component (the name):
How do I query two components, say the age and the name of a person? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
To query for multiple components or to query with multiple filters, you can use a tuple in either position. fn greet_people(
query: Query<(&Name, &Age), (With<Person>, With<SillyHat>)>
) {
for (name, transform) in query.iter() {
/* ... */
}
} |
Beta Was this translation helpful? Give feedback.
-
fn greet_people(query: Query<(&Name, &Age), With<Person>>) {
for (name, age) in query.iter() {
println!("Hello {}! You are {} years old.", name.0, age.0);
}
} You use a tuple. |
Beta Was this translation helpful? Give feedback.
-
Thanks guys, I played around with it and the tuple method worked! |
Beta Was this translation helpful? Give feedback.
Query
takes two generic parameters, a query and a filter.To query for multiple components or to query with multiple filters, you can use a tuple in either position.