diff options
author | HampusM <hampus@hampusmat.com> | 2025-09-23 18:20:11 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-09-23 18:20:23 +0200 |
commit | 20c45467612ff2448b87ce731f939145c6e3a3b1 (patch) | |
tree | c3db1eb3ad2c97810873ef594ac0046e328bbf02 | |
parent | 6e7abf273d758bf15c1ba3e331e370b2bea3f8e2 (diff) |
feat(ecs): make Actions::spawn return future entity's UID
-rw-r--r-- | ecs/src/actions.rs | 17 | ||||
-rw-r--r-- | ecs/src/lib.rs | 4 |
2 files changed, 13 insertions, 8 deletions
diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index 2dd68bf..ba3ced5 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -16,11 +16,18 @@ pub struct Actions<'world> impl<'world> Actions<'world> { - /// Queues up a entity to spawn at the end of the current tick. - pub fn spawn<Comps: ComponentSequence>(&mut self, components: Comps) + /// Queues up a entity to spawn at the end of the current tick, returning the [`Uid`] + /// that the entity will have. + pub fn spawn<Comps: ComponentSequence>(&mut self, components: Comps) -> Uid { - self.action_queue - .push(Action::Spawn(components.into_parts_array().into())); + let new_entity_uid = Uid::new_unique(UidKind::Entity); + + self.action_queue.push(Action::Spawn( + new_entity_uid, + components.into_parts_array().into(), + )); + + new_entity_uid } /// Queues up despawning a entity at the end of the current tick. @@ -149,7 +156,7 @@ impl Ref<'_> #[derive(Debug)] pub(crate) enum Action { - Spawn(Vec<ComponentParts>), + Spawn(Uid, Vec<ComponentParts>), Despawn(Uid), AddComponents(Uid, Vec<ComponentParts>), RemoveComponents(Uid, Vec<Uid>), diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 0c2197c..77d40db 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -425,9 +425,7 @@ impl World for action in action_queue_lock.drain(..) { match action { - Action::Spawn(components) => { - let new_entity_uid = Uid::new_unique(UidKind::Entity); - + Action::Spawn(new_entity_uid, components) => { if let Err(err) = self.data.component_storage.create_entity(new_entity_uid) { |