Add register_component to AppBuilder and improve error message (#1750)

This commit is contained in:
Carter Anderson 2021-03-26 04:15:07 +00:00
parent 500d7469e7
commit 7a511394ac
2 changed files with 18 additions and 4 deletions

View file

@ -5,7 +5,7 @@ use crate::{
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
};
use bevy_ecs::{
component::Component,
component::{Component, ComponentDescriptor},
schedule::{
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
},
@ -308,6 +308,17 @@ impl AppBuilder {
self
}
/// Registers a new component using the given [ComponentDescriptor]. Components do not need to
/// be manually registered. This just provides a way to override default configuration.
/// Attempting to register a component with a type that has already been used by [World]
/// will result in an error.
///
/// See [World::register_component]
pub fn register_component(&mut self, descriptor: ComponentDescriptor) -> &mut Self {
self.world_mut().register_component(descriptor).unwrap();
self
}
#[cfg(feature = "bevy_reflect")]
pub fn register_type<T: bevy_reflect::GetTypeRegistration>(&mut self) -> &mut Self {
{

View file

@ -187,8 +187,8 @@ pub struct Components {
#[derive(Debug, Error)]
pub enum ComponentsError {
#[error("A component of type {0:?} already exists")]
ComponentAlreadyExists(TypeId),
#[error("A component of type {name:?} ({type_id:?}) already exists")]
ComponentAlreadyExists { type_id: TypeId, name: String },
}
impl Components {
@ -200,7 +200,10 @@ impl Components {
if let Some(type_id) = descriptor.type_id {
let index_entry = self.indices.entry(type_id);
if let Entry::Occupied(_) = index_entry {
return Err(ComponentsError::ComponentAlreadyExists(type_id));
return Err(ComponentsError::ComponentAlreadyExists {
type_id,
name: descriptor.name,
});
}
self.indices.insert(type_id, index);
}