Skip to content

Commit acff1a0

Browse files
james-j-obriencart
authored andcommitted
Add get_entity to Commands (bevyengine#5854)
# Objective - Fixes bevyengine#5850 ## Solution - As described in the issue, added a `get_entity` method on `Commands` that returns an `Option<EntityCommands>` ## Changelog - Added the new method with a simple doc test - I have re-used `get_entity` in `entity`, similarly to how `get_single` is used in `single` while additionally preserving the error message - Add `#[inline]` to both functions Entities that have commands queued to despawn system will still return commands when `get_entity` is called but that is representative of the fact that the entity is still around until those commands are flushed. A potential `contains_entity` could also be added in this PR if desired, that would effectively be replacing Entities.contains but may be more discoverable if this is a common use case. Co-authored-by: Carter Anderson <[email protected]>
1 parent 5766dc2 commit acff1a0

File tree

1 file changed

+37
-7
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+37
-7
lines changed

crates/bevy_ecs/src/system/commands/mod.rs

+37-7
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,47 @@ impl<'w, 's> Commands<'w, 's> {
243243
/// }
244244
/// # bevy_ecs::system::assert_is_system(example_system);
245245
/// ```
246+
#[inline]
246247
#[track_caller]
247248
pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> {
248-
assert!(
249-
self.entities.contains(entity),
250-
"Attempting to create an EntityCommands for entity {:?}, which doesn't exist.",
251-
entity
252-
);
253-
EntityCommands {
249+
self.get_entity(entity).unwrap_or_else(|| {
250+
panic!(
251+
"Attempting to create an EntityCommands for entity {:?}, which doesn't exist.",
252+
entity
253+
)
254+
})
255+
}
256+
257+
/// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise `None`.
258+
/// This does not ensure that the commands will succeed as the entity may no longer exist by the time the associated commands are executed.
259+
///
260+
/// # Example
261+
///
262+
/// ```
263+
/// use bevy_ecs::prelude::*;
264+
///
265+
/// #[derive(Component)]
266+
/// struct Label(&'static str);
267+
268+
/// fn example_system(mut commands: Commands) {
269+
/// // Create a new, empty entity
270+
/// let entity = commands.spawn().id();
271+
///
272+
/// // Get the entity if it still exists, which it will in this case
273+
/// if let Some(mut entity_commands) = commands.get_entity(entity) {
274+
/// // adds a single component to the entity
275+
/// entity_commands.insert(Label("hello world"));
276+
/// }
277+
/// }
278+
/// # bevy_ecs::system::assert_is_system(example_system);
279+
/// ```
280+
#[inline]
281+
#[track_caller]
282+
pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option<EntityCommands<'w, 's, 'a>> {
283+
self.entities.contains(entity).then_some(EntityCommands {
254284
entity,
255285
commands: self,
256-
}
286+
})
257287
}
258288

259289
/// Spawns entities to the [`World`] according to the given iterator (or a type that can

0 commit comments

Comments
 (0)