diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-02 20:15:20 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-02 20:15:49 +0100 |
commit | 61dfbc23b9d68b39eee388fd0bc7df4d02da61f9 (patch) | |
tree | 83ad03604bfc509aad46d487c4d5378cb60494c4 /ecs/src/lib.rs | |
parent | bfd8f31854b660a9ba82e0af303280c74adfd9d4 (diff) |
fix(ecs): add check if entity already exists before creating it
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 452f657..3c517dd 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -75,17 +75,27 @@ impl World /// /// # Panics /// Will panic if mutable internal lock cannot be acquired. + #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] pub fn create_entity<Comps>(&mut self, components: Comps) -> EntityUid where Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>, Comps::Out: EventSequence, { - let (_, entity_uid) = self + let entity_uid = EntityUid::new_unique(); + + #[allow(unused_variables)] + if let Err(err) = self .data .component_storage .write_nonblock() .expect("Failed to acquire read-write component storage lock") - .push_entity(EntityUid::new_unique(), components.into_vec()); + .push_entity(entity_uid, components.into_vec()) + { + #[cfg(feature = "debug")] + tracing::error!("Failed to create entity: {err}"); + + return entity_uid; + }; for component_added_event_id in <Comps::Out as EventSequence>::ids().iter() { self.emit_event_by_id(*component_added_event_id); @@ -160,6 +170,7 @@ impl World /// /// # Panics /// Will panic if a mutable internal lock cannot be acquired. + #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] pub fn perform_queued_actions(&self) { let mut active_action_queue = match *self.data.action_queue.active_queue.borrow() @@ -187,8 +198,15 @@ impl World .map(|component| component.id()) .collect::<Vec<_>>(); - component_storage_lock - .push_entity(EntityUid::new_unique(), components); + #[allow(unused_variables)] + if let Err(err) = component_storage_lock + .push_entity(EntityUid::new_unique(), components) + { + #[cfg(feature = "debug")] + tracing::error!("Failed to create entity: {err}"); + + continue; + } drop(component_storage_lock); |