Entities and Components are great for representing complex, query-able groups of data. But most Apps will also require "globally unique" data of some kind. In Bevy ECS, we represent globally unique data using Resources.
Here are some examples of data that could be encoded as Resources:
Let's solve our App's "hello spam" problem by only printing "hello" once every two seconds. We'll do this by using the
resource, which is automatically added to our App via
For simplicity, remove the
hello_world system from your App. This way we only need to adapt the
Resources are accessed in much the same way that we access components. You can access the
Time resource in your system like this:
ResMut pointers provide read and write access (respectively) to resources.
delta_seconds field on
Time gives us the time that has passed since the last update. But in order to run our system once every two seconds, we must track the amount of time that has passed over a series of updates. To make this easier, Bevy provides the
Timer type. Let's create a new Resource for our system to track elapsed time with a
Now all that's left is adding a
GreetTimer Resource to our
cargo run the App. It should now greet people at a reasonable rate.