From 006724938353ee8939d577f85865757e591fddda Mon Sep 17 00:00:00 2001 From: Tekhnae Raav Date: Tue, 27 Jan 2026 15:54:17 -0600 Subject: [PATCH 1/2] feat: add shared_resource shortcuts --- .../bones_bevy_renderer/src/lib.rs | 13 ++++----- .../bones_bevy_renderer/src/rumble.rs | 3 +- .../bones_framework/tests/reset.rs | 4 +-- framework_crates/bones_lib/src/lib.rs | 28 +++++++++++++++---- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/framework_crates/bones_bevy_renderer/src/lib.rs b/framework_crates/bones_bevy_renderer/src/lib.rs index 779099c030..ac29805497 100644 --- a/framework_crates/bones_bevy_renderer/src/lib.rs +++ b/framework_crates/bones_bevy_renderer/src/lib.rs @@ -68,7 +68,7 @@ pub struct BonesGame(pub bones::Game); impl BonesGame { /// Shorthand for [`bones::AssetServer`] typed access to the shared resource pub fn asset_server(&self) -> Option> { - self.0.shared_resource() + self.0.get_shared_resource() } } @@ -166,7 +166,7 @@ impl BonesBevyRenderer { } app.init_resource::(); - if let Some(mut asset_server) = self.game.shared_resource_mut::() { + if let Some(mut asset_server) = self.game.get_shared_resource_mut::() { asset_server.set_game_version(self.game_version); asset_server.set_io(asset_io(&self.asset_dir, &self.packs_dir)); @@ -273,7 +273,7 @@ impl BonesBevyRenderer { } fn egui_ctx_initialized(game: Res) -> bool { - game.shared_resource::().is_some() + game.get_shared_resource::().is_some() } fn assets_are_loaded(game: Res) -> bool { @@ -368,10 +368,9 @@ pub fn handle_asset_changes( mut bevy_egui_textures: ResMut, mut bones_image_ids: ResMut, ) { - if let Some(mut asset_server) = game.shared_resource_mut::() { + if let Some(mut asset_server) = game.get_shared_resource_mut::() { asset_server.handle_asset_changes(|asset_server, handle| { - let mut bones_egui_textures = - game.shared_resource_mut::().unwrap(); + let mut bones_egui_textures = game.shared_resource_mut::(); let Some(mut asset) = asset_server.get_asset_untyped_mut(handle) else { // There was an issue loading the asset. The error will have been logged. return; @@ -394,7 +393,7 @@ pub fn handle_asset_changes( #[cfg(not(target_arch = "wasm32"))] fn handle_exits(game: Res, mut exits: EventWriter) { - if **game.shared_resource::().unwrap() { + if **game.shared_resource::() { exits.send(bevy::app::AppExit); } } diff --git a/framework_crates/bones_bevy_renderer/src/rumble.rs b/framework_crates/bones_bevy_renderer/src/rumble.rs index 27b55437ef..4e7f5b3fa3 100644 --- a/framework_crates/bones_bevy_renderer/src/rumble.rs +++ b/framework_crates/bones_bevy_renderer/src/rumble.rs @@ -12,7 +12,8 @@ pub fn handle_bones_rumble( game: ResMut, mut rumble_requests: EventWriter, ) { - if let Some(mut bones_rumble_requests) = game.shared_resource_mut::() { + if let Some(mut bones_rumble_requests) = game.get_shared_resource_mut::() + { while let Some(request) = bones_rumble_requests.requests.pop_front() { match request { bones::GamepadRumbleRequest::AddRumble { diff --git a/framework_crates/bones_framework/tests/reset.rs b/framework_crates/bones_framework/tests/reset.rs index fe26c3571f..698730c398 100644 --- a/framework_crates/bones_framework/tests/reset.rs +++ b/framework_crates/bones_framework/tests/reset.rs @@ -24,7 +24,7 @@ pub fn startup_system_reset() { game.step(Instant::now()); // Verify startup system ran and incremented only once - assert_eq!(game.shared_resource::().unwrap().0, 1); + assert_eq!(game.shared_resource::().0, 1); // Add command that will trigger reset on next step { @@ -43,7 +43,7 @@ pub fn startup_system_reset() { game.step(Instant::now()); // Shared resource is not included in reset, should be incremented 2nd time - assert_eq!(game.shared_resource::().unwrap().0, 2); + assert_eq!(game.shared_resource::().0, 2); } /// Verify that single success systems run again (until success condition) diff --git a/framework_crates/bones_lib/src/lib.rs b/framework_crates/bones_lib/src/lib.rs index 234309e30f..8f28f5e915 100644 --- a/framework_crates/bones_lib/src/lib.rs +++ b/framework_crates/bones_lib/src/lib.rs @@ -429,8 +429,9 @@ impl Game { self } #[track_caller] - /// Get the shared resource of a given type out of this [`Game`]s shared resources. - pub fn shared_resource(&self) -> Option> { + /// Get the shared resource of a given type out of this [`Game`]s shared + /// resources if it exists. + pub fn get_shared_resource(&self) -> Option> { let res = self .shared_resources .iter() @@ -448,8 +449,9 @@ impl Game { } #[track_caller] - /// Get the shared resource of a given type out of this [`Game`]s shared resources. - pub fn shared_resource_mut(&self) -> Option> { + /// Get the mutable shared resource of a given type out of this [`Game`]s + /// shared resources if it exists. + pub fn get_shared_resource_mut(&self) -> Option> { let res = self .shared_resources .iter() @@ -466,6 +468,22 @@ impl Game { } } + #[track_caller] + /// Get the shared resource of a given type out of this [`Game`]s shared + /// resources. Panics if the resource doesn't exist. + pub fn shared_resource(&self) -> Ref<'_, T> { + self.get_shared_resource() + .expect("shared resource not found") + } + + #[track_caller] + /// Get the mutable shared resource of a given type out of this [`Game`]s + /// shared resources. Panics if it doesn't exist. + pub fn shared_resource_mut(&self) -> RefMut<'_, T> { + self.get_shared_resource_mut() + .expect("shared resource not found") + } + /// Get the shared resource cell of a given type out of this [`Game`]s shared resources. pub fn shared_resource_cell(&self) -> Option> { let res = self @@ -485,7 +503,7 @@ impl Game { { self.insert_shared_resource(T::default()); } - self.shared_resource_mut::().unwrap() + self.shared_resource_mut::() } /// Insert a resource that will be shared across all game sessions. From 929cbde5a1a3b0f4f4c48454b6058a8a167ab230 Mon Sep 17 00:00:00 2001 From: Tekhnae Raav Date: Tue, 27 Jan 2026 17:28:02 -0600 Subject: [PATCH 2/2] fix: scripting demo --- demos/scripting/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/demos/scripting/src/main.rs b/demos/scripting/src/main.rs index ee14e227b0..8ada4088c8 100644 --- a/demos/scripting/src/main.rs +++ b/demos/scripting/src/main.rs @@ -18,7 +18,6 @@ fn main() { let mut game = Game::new(); game.install_plugin(DefaultGamePlugin); game.shared_resource_mut::() - .unwrap() .register_default_assets(); GameMeta::register_schema();