From 61dfbc23b9d68b39eee388fd0bc7df4d02da61f9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 2 Nov 2024 20:15:20 +0100 Subject: fix(ecs): add check if entity already exists before creating it --- ecs/src/lib.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'ecs/src/lib.rs') 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(&mut self, components: Comps) -> EntityUid where Comps: ComponentSequence + TupleReduce, 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 ::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::>(); - 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); -- cgit v1.2.3-18-g5258