Catching duplicate component (types) in entities

attempt/001
Elijah Voigt 1 year ago
parent 780e3aa520
commit b44147604d

@ -1 +1,2 @@
name "Hello world" name "Hello world"
name "Hello bevy"

@ -1,3 +1,5 @@
use std::any::TypeId;
use crate::prelude::*; use crate::prelude::*;
type ParseFn = type ParseFn =
@ -51,7 +53,12 @@ pub(crate) struct SaveScene {
// Ironically we basically want DynamicEntity: https://docs.rs/bevy/latest/bevy/scene/struct.DynamicEntity.html // Ironically we basically want DynamicEntity: https://docs.rs/bevy/latest/bevy/scene/struct.DynamicEntity.html
#[derive(Asset, TypePath, Default)] #[derive(Asset, TypePath, Default)]
pub(crate) struct SaveEntity { pub(crate) struct SaveEntity {
components: Vec<Box<dyn Reflect>>, components: Vec<SaveEntityComponent>,
}
struct SaveEntityComponent {
type_id: TypeId,
data: Box<dyn Reflect>,
} }
impl SaveEntity { impl SaveEntity {
@ -74,8 +81,13 @@ impl SaveEntity {
// Run line against all parsers // Run line against all parsers
for f in fns { for f in fns {
if let Ok(v) = f(&tokens) { if let Ok(c) = f(&tokens) {
entity.components.push(v); // Bundle the Type ID with this entry for auditing purposes
let t = c.get_represented_type_info().unwrap().type_id();
entity.components.push(SaveEntityComponent {
type_id: t,
data: c,
});
good = true; good = true;
} }
} }
@ -88,6 +100,29 @@ impl SaveEntity {
); );
} }
}); });
// Check for duplicate component types and emit an error
// TODO: It would be nice if this emitted a line refernece instead of a parsed struct!
let l = entity.components.len();
(0..l).for_each(|i| {
(i..l).for_each(|j| {
if i != j {
let c1 = &entity.components[i];
let c2 = &entity.components[j];
let t1 = c1.type_id;
let t2 = c2.type_id;
if t1 == t2 {
error!(
"Duplicate components in {:?}: \n\t{:?}\n\t{:?}",
load_context.asset_path(),
c1.data,
c2.data,
);
}
}
});
});
Ok(entity) Ok(entity)
} }
} }
@ -239,8 +274,8 @@ fn spawn_entities(
// Add each component to the entity // Add each component to the entity
debug!("Populating entity with components {:?}", entity); debug!("Populating entity with components {:?}", entity);
save_entity.components.iter().for_each(|component| { save_entity.components.iter().for_each(|item| {
e.insert_reflect(component.clone_value()); e.insert_reflect(item.data.clone_value());
}); });
}); });
} }

Loading…
Cancel
Save