Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot Zero call to timer in Rust/Bevy?

Tags:

rust

bevy

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?

like image 774
BWStearns Avatar asked Nov 11 '21 20:11

BWStearns


1 Answers

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.

like image 95
Netwave Avatar answered Oct 21 '22 02:10

Netwave