Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion demos/scripting/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fn main() {
let mut game = Game::new();
game.install_plugin(DefaultGamePlugin);
game.shared_resource_mut::<AssetServer>()
.unwrap()
.register_default_assets();
GameMeta::register_schema();

Expand Down
13 changes: 6 additions & 7 deletions framework_crates/bones_bevy_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bones::Ref<'_, bones::AssetServer>> {
self.0.shared_resource()
self.0.get_shared_resource()
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ impl BonesBevyRenderer {
}
app.init_resource::<BonesImageIds>();

if let Some(mut asset_server) = self.game.shared_resource_mut::<bones::AssetServer>() {
if let Some(mut asset_server) = self.game.get_shared_resource_mut::<bones::AssetServer>() {
asset_server.set_game_version(self.game_version);
asset_server.set_io(asset_io(&self.asset_dir, &self.packs_dir));

Expand Down Expand Up @@ -273,7 +273,7 @@ impl BonesBevyRenderer {
}

fn egui_ctx_initialized(game: Res<BonesGame>) -> bool {
game.shared_resource::<bones::EguiCtx>().is_some()
game.get_shared_resource::<bones::EguiCtx>().is_some()
}

fn assets_are_loaded(game: Res<BonesGame>) -> bool {
Expand Down Expand Up @@ -368,10 +368,9 @@ pub fn handle_asset_changes(
mut bevy_egui_textures: ResMut<bevy_egui::EguiUserTextures>,
mut bones_image_ids: ResMut<BonesImageIds>,
) {
if let Some(mut asset_server) = game.shared_resource_mut::<bones::AssetServer>() {
if let Some(mut asset_server) = game.get_shared_resource_mut::<bones::AssetServer>() {
asset_server.handle_asset_changes(|asset_server, handle| {
let mut bones_egui_textures =
game.shared_resource_mut::<bones::EguiTextures>().unwrap();
let mut bones_egui_textures = game.shared_resource_mut::<bones::EguiTextures>();
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;
Expand All @@ -394,7 +393,7 @@ pub fn handle_asset_changes(

#[cfg(not(target_arch = "wasm32"))]
fn handle_exits(game: Res<BonesGame>, mut exits: EventWriter<bevy::app::AppExit>) {
if **game.shared_resource::<bones::ExitBones>().unwrap() {
if **game.shared_resource::<bones::ExitBones>() {
exits.send(bevy::app::AppExit);
}
}
3 changes: 2 additions & 1 deletion framework_crates/bones_bevy_renderer/src/rumble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub fn handle_bones_rumble(
game: ResMut<BonesGame>,
mut rumble_requests: EventWriter<BevyGamepadRumbleRequest>,
) {
if let Some(mut bones_rumble_requests) = game.shared_resource_mut::<bones::GamepadsRumble>() {
if let Some(mut bones_rumble_requests) = game.get_shared_resource_mut::<bones::GamepadsRumble>()
{
while let Some(request) = bones_rumble_requests.requests.pop_front() {
match request {
bones::GamepadRumbleRequest::AddRumble {
Expand Down
4 changes: 2 additions & 2 deletions framework_crates/bones_framework/tests/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Counter>().unwrap().0, 1);
assert_eq!(game.shared_resource::<Counter>().0, 1);

// Add command that will trigger reset on next step
{
Expand All @@ -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::<Counter>().unwrap().0, 2);
assert_eq!(game.shared_resource::<Counter>().0, 2);
}

/// Verify that single success systems run again (until success condition)
Expand Down
28 changes: 23 additions & 5 deletions framework_crates/bones_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
/// Get the shared resource of a given type out of this [`Game`]s shared
/// resources if it exists.
pub fn get_shared_resource<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
let res = self
.shared_resources
.iter()
Expand All @@ -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<T: HasSchema>(&self) -> Option<RefMut<'_, T>> {
/// 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<T: HasSchema>(&self) -> Option<RefMut<'_, T>> {
let res = self
.shared_resources
.iter()
Expand All @@ -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<T: HasSchema>(&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<T: HasSchema>(&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<T: HasSchema>(&self) -> Option<AtomicResource<T>> {
let res = self
Expand All @@ -485,7 +503,7 @@ impl Game {
{
self.insert_shared_resource(T::default());
}
self.shared_resource_mut::<T>().unwrap()
self.shared_resource_mut::<T>()
}

/// Insert a resource that will be shared across all game sessions.
Expand Down