| name | bevy-resources |
| description | Reference for Bevy resources — defining, inserting, initializing, reading/writing, and removing global singleton data. |
| metadata | {"crate":"bevy_ecs","bevy":"0.19"} |
Defining a resource
#[derive(Resource)]
struct Score(usize);
Only one per type per World.
Adding resources
app.insert_resource(Score(0));
app.init_resource::<Score>();
Dynamic insertion from a system:
commands.insert_resource(Score(0));
Removing resources
commands.remove_resource::<Score>();
Reading and writing
fn read(score: Res<Score>) { }
fn write(mut score: ResMut<Score>) {
score.0 += 1;
}
Optional resources
Avoid panics when resource might not exist:
fn maybe_score(mut score: Option<ResMut<Score>>) {
if let Some(mut score) = score.as_deref_mut() {
score.0 += 1;
}
}
Prefer init_resource at app definition over dynamic creation to ensure availability.