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

Rename EntityCommands::clone to clone_and_spawn #16696

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 15 additions & 7 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,10 @@ impl<'a> EntityCommands<'a> {

/// Clones an entity and returns the [`EntityCommands`] of the clone.
///
/// The clone will have all the components of the original entity that
/// can be cloned. A component must implement either `Clone` or `Reflect`
/// to be cloned.
///
/// # Panics
///
/// The command will panic when applied if the original entity does not exist.
Expand All @@ -1739,16 +1743,20 @@ impl<'a> EntityCommands<'a> {
/// let mut entity = commands.spawn((ComponentA(10), ComponentB(20)));
///
/// // Create a clone of the first entity
/// let entity_clone = entity.clone();
/// let mut entity_clone = entity.spawn_clone();
/// }
/// # bevy_ecs::system::assert_is_system(example_system);
pub fn clone(&mut self) -> EntityCommands<'_> {
self.clone_with(|_| {})
pub fn spawn_clone(&mut self) -> EntityCommands<'_> {
self.spawn_clone_with(|_| {})
}

/// Clones an entity and allows configuring cloning behavior using [`EntityCloneBuilder`],
/// returning the [`EntityCommands`] of the clone.
///
/// The clone will have all the components of the original entity that
/// can be cloned. A component must implement either `Clone` or `Reflect`
/// to be cloned.
///
/// # Panics
///
/// The command will panic when applied if the original entity does not exist.
Expand All @@ -1768,17 +1776,17 @@ impl<'a> EntityCommands<'a> {
/// let mut entity = commands.spawn((ComponentA(10), ComponentB(20)));
///
/// // Create a clone of the first entity, but without ComponentB
/// let entity_clone = entity.clone_with(|builder| {
/// let mut entity_clone = entity.spawn_clone_with(|builder| {
/// builder.deny::<ComponentB>();
/// });
/// }
/// # bevy_ecs::system::assert_is_system(example_system);
pub fn clone_with(
pub fn spawn_clone_with(
&mut self,
f: impl FnOnce(&mut EntityCloneBuilder) + Send + Sync + 'static,
) -> EntityCommands<'_> {
let entity_clone = self.commands().spawn_empty().id();
self.queue(clone_entity_with(entity_clone, f));
self.queue(spawn_clone_with(entity_clone, f));
EntityCommands {
commands: self.commands_mut().reborrow(),
entity: entity_clone,
Expand Down Expand Up @@ -2258,7 +2266,7 @@ fn observe<E: Event, B: Bundle, M>(
}
}

fn clone_entity_with(
fn spawn_clone_with(
entity_clone: Entity,
f: impl FnOnce(&mut EntityCloneBuilder) + Send + Sync + 'static,
) -> impl EntityCommand {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_hierarchy/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ mod tests {
.id();
let e_clone = commands
.entity(e)
.clone_with(|builder| {
.spawn_clone_with(|builder| {
builder.recursive(true);
})
.id();
Expand Down Expand Up @@ -483,7 +483,7 @@ mod tests {

let child_clone = commands
.entity(child)
.clone_with(|builder| {
.spawn_clone_with(|builder| {
builder.as_child(true);
})
.id();
Expand Down
Loading