In the Bevy book the following code is used:
struct GreetTimer(Timer);
fn greet_people(
time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
// update our timer with the time elapsed since the last update
// if that caused the timer to finish, we say hello to everyone
if timer.0.tick(time.delta()).just_finished() {
for name in query.iter() {
println!("hello {}!", name.0);
}
}
}
What are the timer.0
and name.0
calls doing? The book doesn't address it, and I see that Timer
has a tick method, so what is .0
doing here since timer
is already a Timer
?
It is related to tuples. In rust tuples can be accessed by item position in that way:
let foo: (u32, u32) = (0, 1);
println!("{}", foo.0);
println!("{}", foo.1);
It also happens with some (tuple) structs:
struct Foo(u32);
let foo = Foo(1);
println!("{}", foo.0);
Playground
You can further check some documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With