Add register_component
to AppBuilder and improve error message (#1750)
This commit is contained in:
parent
500d7469e7
commit
7a511394ac
@ -5,7 +5,7 @@ use crate::{
|
|||||||
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
|
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
|
||||||
};
|
};
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
component::Component,
|
component::{Component, ComponentDescriptor},
|
||||||
schedule::{
|
schedule::{
|
||||||
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
|
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
|
||||||
},
|
},
|
||||||
@ -308,6 +308,17 @@ impl AppBuilder {
|
|||||||
self
|
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")]
|
#[cfg(feature = "bevy_reflect")]
|
||||||
pub fn register_type<T: bevy_reflect::GetTypeRegistration>(&mut self) -> &mut Self {
|
pub fn register_type<T: bevy_reflect::GetTypeRegistration>(&mut self) -> &mut Self {
|
||||||
{
|
{
|
||||||
|
@ -187,8 +187,8 @@ pub struct Components {
|
|||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ComponentsError {
|
pub enum ComponentsError {
|
||||||
#[error("A component of type {0:?} already exists")]
|
#[error("A component of type {name:?} ({type_id:?}) already exists")]
|
||||||
ComponentAlreadyExists(TypeId),
|
ComponentAlreadyExists { type_id: TypeId, name: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Components {
|
impl Components {
|
||||||
@ -200,7 +200,10 @@ impl Components {
|
|||||||
if let Some(type_id) = descriptor.type_id {
|
if let Some(type_id) = descriptor.type_id {
|
||||||
let index_entry = self.indices.entry(type_id);
|
let index_entry = self.indices.entry(type_id);
|
||||||
if let Entry::Occupied(_) = index_entry {
|
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);
|
self.indices.insert(type_id, index);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user