Skip to content

Commit 3269a78

Browse files
006-executing-systems-on-specific-intervals
In snake the snake only moves one square every so often. In Bevy we can emulate this with fixed time steps using [`Time`](https://docs.rs/bevy/0.12.0/bevy/prelude/struct.Time.html); specifically a variant of `Time` called [`Fixed`](https://docs.rs/bevy/0.12.0/bevy/time/struct.Fixed.html). In `src/main.rs`, we start off by inserting a new `Resource` using [`Time<Fixed>`](https://docs.rs/bevy/0.12.0/bevy/time/struct.Time.html#impl-Time%3CFixed%3E) to handle counting our time steps. In this case I've chosen to make the step run every 0.1 seconds. ```rust .insert_resource(Time::<Fixed>::from_seconds(0.1)) ``` <details><summary>more about `Time` and `Fixed`</summary> There are a lot of different ways to deal with time. In Bevy, there's ways of accessing [wall-clock time](https://docs.rs/bevy/0.12.0/bevy/time/struct.Real.html), [virtual time](https://docs.rs/bevy/0.12.0/bevy/time/struct.Virtual.html), and [fixed time](https://docs.rs/bevy/0.12.0/bevy/time/struct.Fixed.html). Each of these ways of accessing time share a large set of functions for dealing with time, so it makes sense that we'd have a way to represent all of that shared functionality. Here is the [Pull Request](bevyengine/bevy#8964) that implemented this unified interface. [`Time`](https://docs.rs/bevy/0.12.0/bevy/prelude/struct.Time.html) then, is a type that accepts a generic type argument (in this case we're using [`Fixed`](https://docs.rs/bevy/0.12.0/bevy/time/struct.Fixed.html)). There are additional functions implemented on [`Time<Fixed>`](https://docs.rs/bevy/0.12.0/bevy/time/struct.Time.html#impl-Time%3CFixed%3E) that don't exist on other specializations of `Time`. One such function is `from_seconds` which is used to instantiate a new `Time<Fixed>` value that we can insert as a `Resource`. The syntax for accessing the `from_seconds` function associated with the `Time<Fixed>` type is not written as `Time<Fixed>::from_seconds` but rather is `Time::<Fixed>::from_seconds`. This is mostly because if we didn't use the turbofish syntax then it could be ambiguous as to whether the programmer meant to write "greater than" and "less than" or "a generic type". </details> After creating our `Time<Fixed>` `Resource`, we need to add a system that gets executed on the interval we defined. We'll use the [`FixedUpdate`](https://docs.rs/bevy/0.12.0/bevy/app/struct.FixedUpdate.html) schedule and a new system we haven't created yet called `tick`. ```rust .add_systems(FixedUpdate, tick) ``` The `tick` system will be defined in `src/lib.rs`, so we can write the use item now to bring it into scope. ```rust use snake::{ board::{spawn_board, Board}, snake::{spawn_snake, Snake}, tick, }; ``` In `src/lib.rs` , we can drop in a new function that logs out the word `"tick!"`. This system will run once every 0.1 seconds, as we specified in our `src/main.rs`. ```rust pub mod board; pub mod colors; pub mod snake; use bevy::prelude::*; pub fn tick() { info!("tick!"); } ``` `info!` comes from the [tracing](https://docs.rs/tracing/0.1.40/tracing/index.html) crate, which Bevy includes and sets up for us. > [!NOTE] tracing in Bevy > Bevy handles setting up logging in different ways depending on the environment we're running in using the [`LogPlugin`](https://docs.rs/bevy/0.12.0/bevy/log/struct.LogPlugin.html), which is part of the [`DefaultPlugins`](https://docs.rs/bevy/0.12.0/bevy/struct.DefaultPlugins.html) After running `cargo run`, we can see our new system logging out `"tick"` on the interval we specified. The output will include the timestamp of the log, the log level (`INFO` in this case), any span information, and finally the message we logged out. ``` 2023-11-13T17:54:07.073260Z INFO snake: tick! 2023-11-13T17:54:07.174248Z INFO snake: tick! 2023-11-13T17:54:07.273097Z INFO snake: tick! 2023-11-13T17:54:07.373646Z INFO snake: tick! 2023-11-13T17:54:07.473044Z INFO snake: tick! ``` In the next lesson we'll combine this system with our VecDeque to handle our snake's movement.
1 parent 6cc7a3b commit 3269a78

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
pub mod board;
22
pub mod colors;
33
pub mod snake;
4+
5+
use bevy::prelude::*;
6+
7+
pub fn tick() {
8+
info!("tick!");
9+
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use bevy::prelude::*;
22
use snake::{
33
board::{spawn_board, Board},
44
snake::{spawn_snake, Snake},
5+
tick,
56
};
67

78
fn main() {
@@ -18,10 +19,12 @@ fn main() {
1819
}))
1920
.insert_resource(Board::new(20))
2021
.init_resource::<Snake>()
22+
.insert_resource(Time::<Fixed>::from_seconds(0.1))
2123
.add_systems(
2224
Startup,
2325
(setup, spawn_board, spawn_snake),
2426
)
27+
.add_systems(FixedUpdate, tick)
2528
.run();
2629
}
2730

0 commit comments

Comments
 (0)