From f520e1220423261b62a70ae3a7adaa561bb1d05d Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 11:30:29 -0700 Subject: [PATCH 1/8] Add get_entity to Commands --- crates/bevy_ecs/src/system/commands/mod.rs | 41 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 19c057bc4b664..24362a1c50a48 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -245,14 +245,43 @@ impl<'w, 's> Commands<'w, 's> { /// ``` #[track_caller] pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> { - assert!( - self.entities.contains(entity), + self.get_entity(entity).expect(&format!( "Attempting to create an EntityCommands for entity {:?}, which doesn't exist.", entity - ); - EntityCommands { - entity, - commands: self, + )) + } + + /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise None. + /// + /// # Example + /// + /// ``` + /// use bevy_ecs::prelude::*; + /// + /// #[derive(Component)] + /// struct Label(&'static str); + + /// fn example_system(mut commands: Commands) { + /// // Create a new, empty entity + /// let entity = commands.spawn().id(); + /// + /// // Get the entity if it still exists, which it will in this case + /// if let Some(mut entity_commands) = commands.get_entity(entity) { + /// // adds a single component to the entity + /// entity_commands.insert(Label("hello world")); + /// } + /// } + /// # bevy_ecs::system::assert_is_system(example_system); + /// ``` + #[track_caller] + pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option> { + if self.entities.contains(entity) { + Some(EntityCommands { + entity, + commands: self, + }) + } else { + None } } From 2bce18f9571ef8b628272d13c48e34f4b2bf9770 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 11:47:57 -0700 Subject: [PATCH 2/8] Use .then instead of if --- crates/bevy_ecs/src/system/commands/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 24362a1c50a48..3ead69ca983eb 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -275,14 +275,10 @@ impl<'w, 's> Commands<'w, 's> { /// ``` #[track_caller] pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option> { - if self.entities.contains(entity) { - Some(EntityCommands { - entity, - commands: self, - }) - } else { - None - } + self.entities.contains(entity).then(EntityCommands { + entity, + commands: self, + }) } /// Spawns entities to the [`World`] according to the given iterator (or a type that can From da373a05d9f4d2a3144589c660b785865b71ba12 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 11:59:00 -0700 Subject: [PATCH 3/8] Fix typo --- crates/bevy_ecs/src/system/commands/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 3ead69ca983eb..6586e622ba297 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -275,7 +275,7 @@ impl<'w, 's> Commands<'w, 's> { /// ``` #[track_caller] pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option> { - self.entities.contains(entity).then(EntityCommands { + self.entities.contains(entity).then_some(EntityCommands { entity, commands: self, }) From ef0e36c6ce42002cf0d6569d95aca28b87eed239 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 12:09:57 -0700 Subject: [PATCH 4/8] Get called out by clippy, run ci check --- crates/bevy_ecs/src/system/commands/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 6586e622ba297..3ee3cb53b798e 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -245,10 +245,12 @@ impl<'w, 's> Commands<'w, 's> { /// ``` #[track_caller] pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> { - self.get_entity(entity).expect(&format!( - "Attempting to create an EntityCommands for entity {:?}, which doesn't exist.", - entity - )) + self.get_entity(entity).unwrap_or_else(|| { + panic!( + "Attempting to create an EntityCommands for entity {:?}, which doesn't exist.", + entity + ) + }) } /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise None. From f59fb29f73849d88f66a5441d3d2ab813f6be2ab Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 12:56:02 -0700 Subject: [PATCH 5/8] Inline get_entity and entity --- crates/bevy_ecs/src/system/commands/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 3ee3cb53b798e..7d371973eba58 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -243,6 +243,7 @@ impl<'w, 's> Commands<'w, 's> { /// } /// # bevy_ecs::system::assert_is_system(example_system); /// ``` + #[inline] #[track_caller] pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> { self.get_entity(entity).unwrap_or_else(|| { @@ -275,6 +276,7 @@ impl<'w, 's> Commands<'w, 's> { /// } /// # bevy_ecs::system::assert_is_system(example_system); /// ``` + #[inline] #[track_caller] pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option> { self.entities.contains(entity).then_some(EntityCommands { From 4d5d772eeee50dc7d472938f362cef12d6cda222 Mon Sep 17 00:00:00 2001 From: james-j-obrien Date: Thu, 1 Sep 2022 14:04:13 -0700 Subject: [PATCH 6/8] Improve docs, mention that the entity may be deleted before commands are applied --- crates/bevy_ecs/src/system/commands/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 7d371973eba58..b5b48fc1b6f7f 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -255,6 +255,7 @@ impl<'w, 's> Commands<'w, 's> { } /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise None. + /// This does not ensure that the commands will succeed as the entity may no longer exist by the time they are executed. /// /// # Example /// From 4c3f1f94b47919cdfa834f840a16ec7aabdf73be Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 1 Sep 2022 15:06:16 -0700 Subject: [PATCH 7/8] Update crates/bevy_ecs/src/system/commands/mod.rs Co-authored-by: Jerome Humbert --- crates/bevy_ecs/src/system/commands/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index b5b48fc1b6f7f..6f0470d6af803 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -254,7 +254,7 @@ impl<'w, 's> Commands<'w, 's> { }) } - /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise None. + /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise `None`. /// This does not ensure that the commands will succeed as the entity may no longer exist by the time they are executed. /// /// # Example From b7ed26dca7ec5128011a237201c59d570714bcd2 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 1 Sep 2022 15:06:21 -0700 Subject: [PATCH 8/8] Update crates/bevy_ecs/src/system/commands/mod.rs Co-authored-by: Jerome Humbert --- crates/bevy_ecs/src/system/commands/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 6f0470d6af803..1054538b19c84 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -255,7 +255,7 @@ impl<'w, 's> Commands<'w, 's> { } /// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise `None`. - /// This does not ensure that the commands will succeed as the entity may no longer exist by the time they are executed. + /// This does not ensure that the commands will succeed as the entity may no longer exist by the time the associated commands are executed. /// /// # Example ///