Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add component_id function to World and Components #5066

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
41 changes: 41 additions & 0 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ impl ComponentInfo {
}
}

/// A [`ComponentId`] is an semi-opaque value which uniquely identifies the type of
/// a [`Component`] within a [`World`](crate::world::World).
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
///
/// Each time a new [`Component`] type is registered within a [`World`](crate::world::World)
/// using [`World::init_component`](crate::world::World::init_component) or
/// [`World::init_component_with_descriptor`](crate::world::World::init_component_with_descriptor),
/// a corresponding [`ComponentId`] is created to track it.
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
///
/// While the distinction between [`ComponentId`] and [`TypeId`] may seem superficial, breaking them
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// in to two separate but related concepts allows Bevy components to exist outside of Rust's type system.
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// Each Rust type registered as a [`Component`] will have a corresponding [`ComponentId`], but additional
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// [`ComponentId`]s may exist in a [`World`](crate::world::World) to track components which cannot be
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// represented as Rust types for scripting or other advanced use-cases.
///
/// A [`ComponentId`] is tightly coupled to its parent [`World`](crate::world::World).
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// Attempting to use a [`ComponentId`] from one [`World`](crate::world::World) to access the metadata
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// of a [`Component`] in a different [`World`](crate::world::World) is undefined behaviour and should
/// not be attempted.
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct ComponentId(usize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider talking a bit about the relationship with TypeId here too.

Basically, for Rust types, there's a one-to-one mapping between TypeId and ComponentId. But non-Rust components can be used (well, only kind of so far), and these also get their own ComponentId.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a new section to explain the relationship. I'm not totally happy with the last sentence "Each Rust type ... or other advanced use cases" since it's pretty clunky, but after fiddling with it for a bit I couldn't come up with anything that wasn't quite a bit more verbose. Suggestions appreciated.


Expand Down Expand Up @@ -346,11 +364,34 @@ impl Components {
self.components.get_unchecked(id.0)
}

/// Type-erased equivalent of [`Components::component_id`].
#[inline]
pub fn get_id(&self, type_id: TypeId) -> Option<ComponentId> {
self.indices.get(&type_id).map(|index| ComponentId(*index))
}

/// Retrieves the [`ComponentId`] of the given [`Component`] type in
/// this [`Components`] instance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Retrieves the [`ComponentId`] of the given [`Component`] type in
/// this [`Components`] instance.
/// Retrieves the [`ComponentId`] of the given [`Component`] type.

I think “in this Components instance” can be safely removed because it is implied by the fact this is a Components method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like to convey that this ComponentId is specific to this specific instance of Components without the reader drilling down to the ComponentId docs. I've replaced it with the following for now:

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 [`Component`] type has not
/// yet been initialized using [`Components::init_component`].
/// ```rust
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// use bevy_ecs::prelude::*;
///
/// let mut world = World::new();
///
/// #[derive(Component)]
/// struct ComponentA {}
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
///
/// let component_a_id = world.init_component::<ComponentA>();
///
///assert_eq!(component_a_id, world.components().component_id::<ComponentA>().unwrap())
/// ```
#[inline]
pub fn component_id<T: Component>(&self) -> Option<ComponentId> {
self.get_id(TypeId::of::<T>())
}

#[inline]
pub fn get_resource_id(&self, type_id: TypeId) -> Option<ComponentId> {
self.resource_indices
Expand Down
35 changes: 35 additions & 0 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,23 @@ impl World {
WorldCell::new(self)
}

/// Initializes a new [`Component`] type within this [`World`] and returns the
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// [`ComponentId`] assigned to it.
pub fn init_component<T: Component>(&mut self) -> ComponentId {
self.components.init_component::<T>(&mut self.storages)
}

/// Initializes a new Component type within this [`World`] and returns the
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// [`ComponentId`] assigned to it.
///
/// [`World::init_component_with_descriptor`] differs from [`World::init_component`] in
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// that it uses a [`ComponentDescriptor`] to initialize the new component type instead
/// of statically available type information. This enables the dynamic initialization of
/// new component definitions at runtime for advanced use cases.
///
/// While [`World::init_component_with_descriptor`] is useful in type-erased contexts,
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// the standard [`World::init_component`] function should always be used instead
/// when type information is available at compile time.
pub fn init_component_with_descriptor(
&mut self,
descriptor: ComponentDescriptor,
Expand All @@ -189,6 +202,28 @@ impl World {
.init_component_with_descriptor(&mut self.storages, descriptor)
}

/// Retrieves the [`ComponentId`] of the given [`Component`] type in
/// this [`World`].
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
///
/// Returns [`None`] if the [`Component`] type has not
/// yet been initialized within the [`World`] using [`World::init_component`].
/// ```rust
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// use bevy_ecs::prelude::*;
///
/// let mut world = World::new();
///
/// #[derive(Component)]
/// struct ComponentA {}
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
///
/// let component_a_id = world.init_component::<ComponentA>();
///
///assert_eq!(component_a_id, world.component_id::<ComponentA>().unwrap())
GarettCooper marked this conversation as resolved.
Show resolved Hide resolved
/// ```
#[inline]
pub fn component_id<T: Component>(&self) -> Option<ComponentId> {
self.components.component_id::<T>()
}

/// Retrieves an [`EntityRef`] that exposes read-only operations for the given `entity`.
/// This will panic if the `entity` does not exist. Use [`World::get_entity`] if you want
/// to check for entity existence instead of implicitly panic-ing.
Expand Down