Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding an method to retrieve entity optionally #3840

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,55 @@ impl<'w, 's> Commands<'w, 's> {
}
}

/// Returns an [`EntityCommands`] for the requested [`Entity`].
///
/// In case of a nonexisting entity, a [`None`] is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// In case of a nonexisting entity, a [`None`] is
/// If the entity does not exist at the time this command is evaluated, [`None`] is

/// returned instead.
///
/// # Example
///
/// ```
/// use bevy_ecs::prelude::*;
///
/// #[derive(Component)]
/// struct Label(&'static str);
/// #[derive(Component)]
/// struct Strength(u32);
/// #[derive(Component)]
/// struct Agility(u32);

Copy link
Member

@alice-i-cecile alice-i-cecile Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
///

/// fn example_system(mut commands: Commands) {
/// // Create a new entity with the needed component bundle
/// let entity = commands
/// .spawn_bundle((Strength(1), Agility(2)))
/// .id();
/// // Check (most likely in another System) if the entity still/already exists
Copy link
Member

@alice-i-cecile alice-i-cecile Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is unclear / seems wrong; based on the code, it seems like this will be evaluated as part of the command processing automatically. That is in fact much more useful; it allows users to write commands that fail safely if the chosen entity does not exist.

EDIT: no, this is wrong.

/// if let Some(entity_builder) = commands.get_entity(entity) {
/// entity_builder
/// // adds a new component to the entity
/// .insert(Label("hello world"));
/// } else {
/// commands
/// // adds the needed component bundle to the entity
/// .spawn_bundle((Strength(1), Agility(2)))
/// //adds the new component to the entity
/// .insert(Label("hello world"));
/// }
/// }
/// # example_system.system();
/// ```
#[track_caller]
pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option<EntityCommands<'w, 's, 'a>> {
if self.entities.contains(entity) {
Some(EntityCommands {
entity,
commands: self,
})
} else {
None
}
}

/// Spawns entities to the [`World`] according to the given iterator (or a type that can
/// be converted to it).
///
Expand Down