From 6fbd585d789eb4f6156c0ae0564851021a5a1478 Mon Sep 17 00:00:00 2001 From: pablo-lua <126117294+pablo-lua@users.noreply.github.com> Date: Fri, 19 Jan 2024 03:03:27 -0300 Subject: [PATCH] Fix gizmos app new panic (#11420) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective After the Gizmos changes, `App::init_gizmos_group` turned into a important function that for sure mustn't panic. The problem is: the actual implementation causes a panic if somehow the code is runned before `GizmoPlugin` was added to the App - The error occurs here for example: ```rust fn main() { App::new() .init_gizmo_group::() .add_plugins(DefaultPlugins) .run(); } #[derive(Default, Reflect, GizmoConfigGroup)] struct MyGizmoConfig; ``` ![image](https://github.com/bevyengine/bevy/assets/126117294/35e75608-0946-4320-8035-00a82562e37e) ## Solution - Instead of panicking when getting `GizmoConfigStore`, insert the store in `App::init_gizmos_group` if needed --- ## Changelog ### Changed - Changed App::init_gizmos_group to insert the resource if it don't exist ### Removed - Removed explicit init of `GizmoConfigStore` --------- Co-authored-by: François --- crates/bevy_gizmos/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index daeed30990..684c4ddd29 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -103,7 +103,7 @@ impl Plugin for GizmoPlugin { .init_asset::() .add_plugins(RenderAssetPlugin::::default()) .init_resource::() - .init_resource::() + // We insert the Resource GizmoConfigStore into the world implicitly here if it does not exist. .init_gizmo_group::() .add_plugins(AabbGizmoPlugin); @@ -158,7 +158,7 @@ impl AppGizmoBuilder for App { .add_systems(Last, update_gizmo_meshes::); self.world - .resource_mut::() + .get_resource_or_insert_with::(Default::default) .register::(); let Ok(render_app) = self.get_sub_app_mut(RenderApp) else {