-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
/// returned instead. | ||||||
/// | ||||||
/// # Example | ||||||
/// | ||||||
/// ``` | ||||||
/// use bevy_ecs::prelude::*; | ||||||
/// | ||||||
/// #[derive(Component)] | ||||||
/// struct Label(&'static str); | ||||||
/// #[derive(Component)] | ||||||
/// struct Strength(u32); | ||||||
/// #[derive(Component)] | ||||||
/// struct Agility(u32); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||||||
/// | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.