From 847d573ce663808cb7ada1c5a06214b6a1381e97 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 Jan 2023 20:56:03 -0500 Subject: [PATCH 1/6] Added `resource_id` --- crates/bevy_ecs/src/component.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 39f89b95ffaaf..44206c8380404 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -498,6 +498,7 @@ impl Components { self.get_id(TypeId::of::()) } + /// Type-erased equivalent of [`Components::resource_id`]. #[inline] pub fn get_resource_id(&self, type_id: TypeId) -> Option { self.resource_indices @@ -505,6 +506,31 @@ impl Components { .map(|index| ComponentId(*index)) } + /// Returns the [`ComponentId`] of the given [`Resource`] type `T`. + /// + /// The returned `ComponentId` is specific to the `Components instance` + /// it was retrieved from and should not be used with another `Components` + /// instance. + /// + /// Returns [`None`] if the `Resource` type has not + /// yet been initialized using [`Components::init_resource`]. + /// + /// ```rust + /// use bevy_ecs::prelude::*; + /// + /// let mut world = World::new(); + /// + /// #[derive(Resource)] + /// struct ResourceA; + /// + /// let resource_a_id = world.init_resource::(); + /// + /// assert_eq!(resource_a_id, world.components().resource_id::().unwrap()) + /// ``` + pub fn resource_id(&self) -> Option { + self.get_resource_id(TypeId::of::()) + } + #[inline] pub fn init_resource(&mut self) -> ComponentId { // SAFETY: The [`ComponentDescriptor`] matches the [`TypeId`] From a2a78b62f7ad8f31d4a5b285639b530226493af9 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 Jan 2023 20:59:44 -0500 Subject: [PATCH 2/6] Removed a space for `cargo fmt` --- crates/bevy_ecs/src/component.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 44206c8380404..9ab79f381b95e 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -507,7 +507,7 @@ impl Components { } /// Returns the [`ComponentId`] of the given [`Resource`] type `T`. - /// + /// /// The returned `ComponentId` is specific to the `Components instance` /// it was retrieved from and should not be used with another `Components` /// instance. From a8f9d900be9ef6c3d3fc0f54c977d85881c3a311 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 Jan 2023 22:51:49 -0500 Subject: [PATCH 3/6] `init_resource` returns `ComponentId` --- crates/bevy_ecs/src/component.rs | 2 +- crates/bevy_ecs/src/world/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 9ab79f381b95e..22e1027da146e 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -520,7 +520,7 @@ impl Components { /// /// let mut world = World::new(); /// - /// #[derive(Resource)] + /// #[derive(Resource, Default)] /// struct ResourceA; /// /// let resource_a_id = world.init_resource::(); diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 7fe6aecd2bd8d..01670325307e2 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -777,7 +777,7 @@ impl World { /// Note that any resource with the `Default` trait automatically implements `FromWorld`, /// and those default values will be here instead. #[inline] - pub fn init_resource(&mut self) { + pub fn init_resource(&mut self) -> ComponentId { let component_id = self.components.init_resource::(); if self .storages @@ -793,6 +793,7 @@ impl World { } }); } + component_id } /// Inserts a new resource with the given `value`. From 7897308b29c086b53784b41fad6826d95f511ef9 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:42:30 -0500 Subject: [PATCH 4/6] Update crates/bevy_ecs/src/component.rs Co-authored-by: James Liu --- crates/bevy_ecs/src/component.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 22e1027da146e..c8b7d852df302 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -527,6 +527,7 @@ impl Components { /// /// assert_eq!(resource_a_id, world.components().resource_id::().unwrap()) /// ``` + #[inline] pub fn resource_id(&self) -> Option { self.get_resource_id(TypeId::of::()) } From d30a02b88a7ba43447afeb70c10f29c8a3ec83cd Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Fri, 20 Jan 2023 10:50:04 -0500 Subject: [PATCH 5/6] Update crates/bevy_ecs/src/component.rs Co-authored-by: JoJoJet <21144246+JoJoJet@users.noreply.github.com> --- crates/bevy_ecs/src/component.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index c8b7d852df302..6e8097584317a 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -508,7 +508,7 @@ impl Components { /// Returns the [`ComponentId`] of the given [`Resource`] type `T`. /// - /// The returned `ComponentId` is specific to the `Components instance` + /// The returned `ComponentId` is specific to the `Components` instance /// it was retrieved from and should not be used with another `Components` /// instance. /// From 28871ad180c3205d325c621ee5034b5a39fcb7f7 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Fri, 20 Jan 2023 11:01:46 -0500 Subject: [PATCH 6/6] Changed `init_non_send_resource` --- crates/bevy_ecs/src/world/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 01670325307e2..dd09a5341aaa4 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -769,12 +769,12 @@ impl World { } } - /// Inserts a new resource with standard starting values. + /// Initializes a new resource and returns the [`ComponentId`] created for it. /// /// If the resource already exists, nothing happens. /// /// The value given by the [`FromWorld::from_world`] method will be used. - /// Note that any resource with the `Default` trait automatically implements `FromWorld`, + /// Note that any resource with the [`Default`] trait automatically implements [`FromWorld`], /// and those default values will be here instead. #[inline] pub fn init_resource(&mut self) -> ComponentId { @@ -812,7 +812,7 @@ impl World { }); } - /// Inserts a new non-send resource with standard starting values. + /// Initializes a new non-send resource and returns the [`ComponentId`] created for it. /// /// If the resource already exists, nothing happens. /// @@ -824,7 +824,7 @@ impl World { /// /// Panics if called from a thread other than the main thread. #[inline] - pub fn init_non_send_resource(&mut self) { + pub fn init_non_send_resource(&mut self) -> ComponentId { let component_id = self.components.init_non_send::(); if self .storages @@ -840,6 +840,7 @@ impl World { } }); } + component_id } /// Inserts a new non-send resource with the given `value`.