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

Add clone_components and move_components (and variants) to EntityWorldMut and EntityCommands #16826

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JaySpruce
Copy link
Contributor

Objective

Thanks to @eugineerd's work on entity cloning (#16132), we now have a robust way to copy components between entities. We can leverage this to implement some useful methods that would have been more complicated before.

Closes #15350.

Solution

Added the following methods to EntityWorldMut and EntityCommands:

  • clone_components
  • clone_components_with_requires
  • clone_components_by_id
  • move_components
  • move_components_with_requires
  • move_components_by_id

Full syntax:

  • clone_components::<B: Bundle>(target: Entity)
  • clone_components_with_requires::<B: Bundle>(target: Entity)
  • clone_components_by_id(target: Entity, ids: impl IntoIterator<Item = ComponentId>)
  • move_components::<B: Bundle>(target: Entity)
  • move_components_with_requires::<B: Bundle>(target: Entity)
  • move_components_by_id(target: Entity, ids: impl IntoIterator<Item = ComponentId> + Clone)

Also added clone_and_spawn and clone_and_spawn_with to EntityWorldMut (EntityCommands already had them).

Showcase

assert_eq!(world.entity(entity_a).get::<B>(), Some(&B));
assert_eq!(world.entity(entity_b).get::<B>(), None);
world.entity_mut(entity_a).clone_components::<B>(entity_b);
assert_eq!(world.entity(entity_a).get::<B>(), Some(&B));
assert_eq!(world.entity(entity_b).get::<B>(), Some(&B));

assert_eq!(world.entity(entity_a).get::<C>(), Some(&C(5)));
assert_eq!(world.entity(entity_b).get::<C>(), None);
world.entity_mut(entity_a).move_components::<C>(entity_b);
assert_eq!(world.entity(entity_a).get::<C>(), None);
assert_eq!(world.entity(entity_b).get::<C>(), Some(&C(5)));

Question

Should there be clone_components_by_id_with_requires and move_components_by_id_with_requires? They should be simple to add, but it doesn't look like other methods have them (there's remove_by_id and remove_with_requires, but no remove_by_id_with_requires).

Also, I'm not super familiar with the usage of dynamic components, so I'm less confident on the implementation there.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 15, 2024
@alice-i-cecile alice-i-cecile modified the milestones: 0.15.1, 0.16 Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Moving components from one entity to another
2 participants