diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 39f89b95ffaaf..6e8097584317a 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,32 @@ 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, Default)] + /// struct ResourceA; + /// + /// let resource_a_id = world.init_resource::(); + /// + /// assert_eq!(resource_a_id, world.components().resource_id::().unwrap()) + /// ``` + #[inline] + 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`] diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 7fe6aecd2bd8d..dd09a5341aaa4 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -769,15 +769,15 @@ 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) { + 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`. @@ -811,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. /// @@ -823,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 @@ -839,6 +840,7 @@ impl World { } }); } + component_id } /// Inserts a new non-send resource with the given `value`.