Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1.45 KB

updating_resources.md

File metadata and controls

48 lines (37 loc) · 1.45 KB

Updating Resources

Resource can also be updated in systems. Similar to Res<MyResource>, we use ResMut<MyResource> as a parameter of a system to have a mutable MyResource, where MyResource is the name of our Resource. Then we can modify the MyResource in the system. Modifying a parameter of type ResMut<...> is like modifying a reference.

Note: Do not forget to add mut before the parameter name of ResMut<...> types.

use bevy::{
    app::{App, Startup},
    ecs::{
        schedule::IntoSystemConfigs,
        system::{Res, ResMut, Resource},
    },
};

fn main() {
    App::new()
        .insert_resource(MyResource { value: 10 })
        .add_systems(Startup, (increase_value, output_value).chain())
        .run();
}

#[derive(Resource)]
struct MyResource {
    value: u32,
}

fn increase_value(mut r: ResMut<MyResource>) {
    r.value += 1;
}

fn output_value(r: Res<MyResource>) {
    println!("{}", r.value);
}

Output:

11

➡️ Next: Removing Resources

📘 Back: Table of contents