diff options
| author | HampusM <hampus@hampusmat.com> | 2026-05-02 17:30:03 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-05-02 17:30:03 +0200 |
| commit | e698922e86c217a261db114ce0392060503f0013 (patch) | |
| tree | 9c879601a1fccf5d6126d62fd4e05fd3965cad93 | |
| parent | aa02cdecfbfc2b2513e37ef72c720477d30a310c (diff) | |
feat(ecs): allow creation of already existing entities
| -rw-r--r-- | ecs/src/component/storage.rs | 11 | ||||
| -rw-r--r-- | ecs/src/lib.rs | 19 |
2 files changed, 16 insertions, 14 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index d974967..dc38b6a 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -145,12 +145,12 @@ impl Storage Some(self.graph.get_node_by_id(id)?.archetype()) } - pub fn create_entity(&mut self, uid: Uid) -> Result<(), Error> + pub fn create_entity(&mut self, uid: Uid) -> Result<(), EntityAlreadyExistsError> { debug_assert_eq!(uid.kind(), UidKind::Entity); if self.entity_archetype_lookup.contains_key(&uid) { - return Err(Error::EntityAlreadyExists(uid)); + return Err(EntityAlreadyExistsError); } let empty_archetype_id = ArchetypeId::new_empty(); @@ -736,9 +736,6 @@ impl ArchetypeRefIter<'_, '_> #[derive(Debug, thiserror::Error)] pub enum Error { - #[error("Entity with ID {0:?} already exists")] - EntityAlreadyExists(Uid), - #[error("Entity with ID {0:?} does not exist")] EntityDoesNotExist(Uid), @@ -755,6 +752,10 @@ pub enum Error }, } +#[derive(Debug, thiserror::Error)] +#[error("Entity with already exists")] +pub struct EntityAlreadyExistsError; + #[derive(Debug)] struct ImaginaryArchetype { diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index f6fba64..7a2178e 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -1,17 +1,17 @@ #![deny(clippy::all, clippy::pedantic)] -use std::any::{type_name, Any, TypeId}; +use std::any::{Any, TypeId, type_name}; use std::fmt::Debug; use std::mem::ManuallyDrop; use std::rc::Rc; -use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use hashbrown::HashMap; use crate::actions::Action; use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent; -use crate::component::storage::Storage as ComponentStorage; +use crate::component::storage::{EntityAlreadyExistsError, Storage as ComponentStorage}; use crate::component::{ Component, IntoParts as IntoComponentParts, @@ -25,19 +25,19 @@ use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::Lock; use crate::pair::{ChildOf, DependsOn, Pair}; use crate::phase::{ - Phase, POST_UPDATE as POST_UPDATE_PHASE, PRE_UPDATE as PRE_UPDATE_PHASE, + Phase, START as START_PHASE, UPDATE as UPDATE_PHASE, }; use crate::query::flexible::Query as FlexibleQuery; use crate::query::{ + MAX_TERM_CNT as QUERY_MAX_TERM_CNT, TermWithFieldTuple as QueryTermWithFieldTuple, TermWithoutFieldTuple as QueryTermWithoutFieldTuple, Terms as QueryTerms, TermsBuilderInterface, - MAX_TERM_CNT as QUERY_MAX_TERM_CNT, }; use crate::sole::{Single, Sole}; use crate::stats::Stats; @@ -332,9 +332,10 @@ impl World { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - if let Err(err) = self.data.component_storage.create_entity(entity_uid) { - tracing::warn!("Failed to create entity: {err}"); - return; + if let Err(EntityAlreadyExistsError) = + self.data.component_storage.create_entity(entity_uid) + { + // This is fine } Self::add_entity_components( @@ -625,7 +626,7 @@ impl<'a> EntityComponentRef<'a> } fn new(component_id: Uid, comp: &'a ArchetypeEntityComponent, entity_id: Uid) - -> Self + -> Self { Self { component_id, |
