-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add MoveBundle command #3717
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 MoveBundle command #3717
Conversation
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.
We also want a version to move a single component. The normal scheme would be to call that move
:
fn move<C: Component>(&mut self,target: Entity) {
self.move_bundle::<(C,)>(target)
}
And obviously these need some docs
But otherwise this looks good to me. Ideally we'd have an example of using this somewhere, but I don't know that any of our examples would want this.
/// } | ||
/// } | ||
/// ``` | ||
pub fn r#move<C: Component>(&mut self, target: Entity) -> &mut Self { |
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.
What should we do about r#move
?
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.
One possible side step here is to name these move_to
and move_bundle_to
.
#3820 could then be clone_to
and clone_bundle_to
.
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.
This would also allow for the addition of move_from
, which would take the source
Entity
as an argument.
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.
IMO move_to
is probably better. And then do the same with clone_to
.
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.
@alice-i-cecile Will do, I'm also happy with move_onto
, if you'd prefer that?
d104951
to
da17cc9
Compare
def26d4
to
2c769cb
Compare
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.
Fix up the names then this LGTM.
IMO we should add the |
@alice-i-cecile Should I link to the |
f3f29d9
to
cb07eff
Compare
@@ -512,6 +512,95 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> { | |||
self | |||
} | |||
|
|||
/// Moves a [`Bundle`] to a target entity. |
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.
/// {Moves/Clones} a [`{Component/Bundle}`] {to/from} a {target/source} entity.
I've picked this for all the documentation across the MoveBundle
and CloneBundle
PR. If anyone has some better suggestions I can change it because, as is, this is pretty aburpt.
9381fed
to
f807e2f
Compare
let bundle_opt = if let Some(mut source) = world.get_entity_mut(self.source) { | ||
source.remove_bundle::<T>() | ||
} else { | ||
panic!("Could not move a bundle (of type `{}`) from entity {:?} because it doesn't exist in this World.\n\ |
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.
Nit: can we extract a function to do this panic instead of copying the message?
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.
There's a slight difference between the two:
from entity
and
to entity
Which means it might be a bit gnarly. e.g.
fn panic_fmt<Ty>(verb: &'static str, preposition: &'static str, entity: Entity) {
panic!("Could not {} a bundle (of type `{}`) {} entity {:?} because it doesn't exist in this World.\n\
If this command was added to a newly spawned entity, ensure that you have not despawned that entity within the same stage.\n\
This may have occurred due to system order ambiguity, or if the spawning system has multiple command buffers",
verb, std::any::type_name::<Ty>(), preposition, entity);
}
Ye/nay?
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.
Yeah I like this.
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.
@alice-i-cecile @B-Reif 8bc7023 hows that look?
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.
I can do it for https://github.com/bevyengine/bevy/pull/3717/files#diff-ebaeb267b15dfe866ccce4f60af1f90f8fd44f31fa1b6ac7529f4b41f38b743bR576 too if you'd like?
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.
Yes please; let's keep this consistent.
72a9056
to
8bc7023
Compare
let bundle = if let Some(some) = bundle_opt { | ||
some | ||
} else { | ||
return; |
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.
should it panic in this case?
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.
If we're being consistent, yes.
I dislike this behavior, but that's for a different PR.
@hlb8122 do you want to clean this up so we can merge it? |
This looks like it has gone stale, and required components change the landscape here significantly. Closing as not planned. |
I still think this is a compelling idea, but I agree, this needs to be completely remade. |
Objective
Moving one component from a entity to another entity is a common operation. It would make sense if this was supported out the box.
Solution
Create a
move_bundle
API onEntityCommands
which results in aMoveBundle
command. TheMoveBundle
command peformsEntityMut::remove_bundle::<T>
on thesource
entity and thenEntityMut::insert_bundle::<T>
on thetarget
entity.Possible Additions
It might be reasonable to also add a
CloneBundle
which does the same except replacesremove::<T>
withget::<T>
then aclone
.