Skip to content

Commit db4c468

Browse files
authored
Rename EntityCommands::clone to clone_and_spawn (#16696)
## Objective Follow-up to #16672. `EntityCommands::clone` looks the same as the `Clone` trait, which could be confusing. A discord discussion has made me realize that's probably a bigger problem than I thought. Oops :P ## Solution Renamed `EntityCommands::clone` to `EntityCommands::clone_and_spawn`, renamed `EntityCommands::clone_with` to `EntityCommands::clone_and_spawn_with`. Also added some docs explaining the commands' relation to `Clone` (components need to implement it (or `Reflect`)). ## Showcase ``` // Create a new entity and keep its EntityCommands let mut entity = commands.spawn((ComponentA(10), ComponentB(20))); // Create a clone of the first entity let mut entity_clone = entity.clone_and_spawn(); ``` ## The Bikeshed - `clone_and_spawn` (Alice's suggestion) - `spawn_clone` (benfrankel's suggestion) - `spawn_cloned` (rparrett's suggestion)
1 parent 1c86cb5 commit db4c468

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,12 @@ impl<'a> EntityCommands<'a> {
17201720

17211721
/// Clones an entity and returns the [`EntityCommands`] of the clone.
17221722
///
1723+
/// The clone will receive all the components of the original that implement
1724+
/// [`Clone`] or [`Reflect`](bevy_reflect::Reflect).
1725+
///
1726+
/// To configure cloning behavior (such as only cloning certain components),
1727+
/// use [`EntityCommands::clone_and_spawn_with`].
1728+
///
17231729
/// # Panics
17241730
///
17251731
/// The command will panic when applied if the original entity does not exist.
@@ -1735,20 +1741,27 @@ impl<'a> EntityCommands<'a> {
17351741
/// struct ComponentB(u32);
17361742
///
17371743
/// fn example_system(mut commands: Commands) {
1738-
/// // Create a new entity and keep its EntityCommands.
1744+
/// // Create a new entity and keep its EntityCommands
17391745
/// let mut entity = commands.spawn((ComponentA(10), ComponentB(20)));
17401746
///
17411747
/// // Create a clone of the first entity
1742-
/// let entity_clone = entity.clone();
1748+
/// let mut entity_clone = entity.clone_and_spawn();
17431749
/// }
17441750
/// # bevy_ecs::system::assert_is_system(example_system);
1745-
pub fn clone(&mut self) -> EntityCommands<'_> {
1746-
self.clone_with(|_| {})
1751+
pub fn clone_and_spawn(&mut self) -> EntityCommands<'_> {
1752+
self.clone_and_spawn_with(|_| {})
17471753
}
17481754

17491755
/// Clones an entity and allows configuring cloning behavior using [`EntityCloneBuilder`],
17501756
/// returning the [`EntityCommands`] of the clone.
17511757
///
1758+
/// By default, the clone will receive all the components of the original that implement
1759+
/// [`Clone`] or [`Reflect`](bevy_reflect::Reflect).
1760+
///
1761+
/// To exclude specific components, use [`EntityCloneBuilder::deny`].
1762+
/// To only include specific components, use [`EntityCloneBuilder::deny_all`]
1763+
/// followed by [`EntityCloneBuilder::allow`].
1764+
///
17521765
/// # Panics
17531766
///
17541767
/// The command will panic when applied if the original entity does not exist.
@@ -1764,21 +1777,21 @@ impl<'a> EntityCommands<'a> {
17641777
/// struct ComponentB(u32);
17651778
///
17661779
/// fn example_system(mut commands: Commands) {
1767-
/// // Create a new entity and keep its EntityCommands.
1780+
/// // Create a new entity and keep its EntityCommands
17681781
/// let mut entity = commands.spawn((ComponentA(10), ComponentB(20)));
17691782
///
17701783
/// // Create a clone of the first entity, but without ComponentB
1771-
/// let entity_clone = entity.clone_with(|builder| {
1784+
/// let mut entity_clone = entity.clone_and_spawn_with(|builder| {
17721785
/// builder.deny::<ComponentB>();
17731786
/// });
17741787
/// }
17751788
/// # bevy_ecs::system::assert_is_system(example_system);
1776-
pub fn clone_with(
1789+
pub fn clone_and_spawn_with(
17771790
&mut self,
17781791
f: impl FnOnce(&mut EntityCloneBuilder) + Send + Sync + 'static,
17791792
) -> EntityCommands<'_> {
17801793
let entity_clone = self.commands().spawn_empty().id();
1781-
self.queue(clone_entity_with(entity_clone, f));
1794+
self.queue(clone_and_spawn_with(entity_clone, f));
17821795
EntityCommands {
17831796
commands: self.commands_mut().reborrow(),
17841797
entity: entity_clone,
@@ -2258,7 +2271,7 @@ fn observe<E: Event, B: Bundle, M>(
22582271
}
22592272
}
22602273

2261-
fn clone_entity_with(
2274+
fn clone_and_spawn_with(
22622275
entity_clone: Entity,
22632276
f: impl FnOnce(&mut EntityCloneBuilder) + Send + Sync + 'static,
22642277
) -> impl EntityCommand {

crates/bevy_hierarchy/src/hierarchy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ mod tests {
444444
.id();
445445
let e_clone = commands
446446
.entity(e)
447-
.clone_with(|builder| {
447+
.clone_and_spawn_with(|builder| {
448448
builder.recursive(true);
449449
})
450450
.id();
@@ -483,7 +483,7 @@ mod tests {
483483

484484
let child_clone = commands
485485
.entity(child)
486-
.clone_with(|builder| {
486+
.clone_and_spawn_with(|builder| {
487487
builder.as_child(true);
488488
})
489489
.id();

0 commit comments

Comments
 (0)