Add clone_components
and move_components
(and variants) to EntityWorldMut
and EntityCommands
#16826
+467
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
andEntityCommands
: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
andclone_and_spawn_with
toEntityWorldMut
(EntityCommands
already had them).Showcase
Question
Should there be
clone_components_by_id_with_requires
andmove_components_by_id_with_requires
? They should be simple to add, but it doesn't look like other methods have them (there'sremove_by_id
andremove_with_requires
, but noremove_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.