--- 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 ```rust #[derive(Resource)] struct Score(usize); ``` A `Resource` is actually a `Component` that belongs to a singleton entity, so resources can be queried like any other component. There can only ever be one instance of a resource type per `World`. ## Adding resources ```rust // With an instance app.insert_resource(Score(0)); // With Default or FromWorld app.init_resource::(); ``` Dynamic insertion from a system: ```rust commands.insert_resource(Score(0)); ``` ## Removing resources ```rust commands.remove_resource::(); ``` ## Reading and writing ```rust fn read(score: Res) { } fn write(mut score: ResMut) { score.0 += 1; } ``` ## Optional resources Avoid panics when resource might not exist: ```rust fn maybe_score(mut score: Option>) { if let Some(mut score) = score.as_deref_mut() { score.0 += 1; } } ``` Prefer `init_resource` at app definition over dynamic creation to ensure availability.