-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Insert children #17558
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
Insert children #17558
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
Fixes #17478 |
@@ -185,6 +185,33 @@ impl<'w> EntityWorldMut<'w> { | |||
self.add_related::<ChildOf>(children) | |||
} | |||
|
|||
/// Insert children at specific index |
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 happens if the entity is already a child? As a user, I'd like to know whether that is okay ok or not. For example, does doing so reorder it?
); | ||
} | ||
children_component.0.reserve(children.len()); | ||
let mut v = children_component.0.split_off(index); |
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 does not update the other side of the relationship (ChildOf
), invalidating the assumptions of the system as a whole.
@bjoernp116 this is a blocker to 0.16; are you interested in finishing this up in the next week or two? Totally fine if not; we can always get someone else to finish this. |
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self { | ||
let parent = self.id(); | ||
if children.contains(&parent) { | ||
panic!("Cannot insert entity as a child of itself."); |
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.
Is it possible to change the return to Bevy Result and then return Err()
here?
Closing as adopted <3 Thanks for getting started on this! |
fixes #17478 # Objective - Complete #17558. - the `insert_children` method was previously removed, and as #17478 points out, needs to be added back. ## Solution - Add a `OrderedRelationshipSourceCollection`, which allows sorting, ordering, rearranging, etc of a `RelationshipSourceCollection`. - Implement `insert_related` - Implement `insert_children` - Tidy up some docs while I'm here. ## Testing @bjoernp116 set up a unit test, and I added a doc test to `OrderedRelationshipSourceCollection`. --------- Co-authored-by: bjoernp116 <[email protected]> Co-authored-by: Dmytro Banin <[email protected]> Co-authored-by: Talin <[email protected]>
fixes #17478 # Objective - Complete #17558. - the `insert_children` method was previously removed, and as #17478 points out, needs to be added back. ## Solution - Add a `OrderedRelationshipSourceCollection`, which allows sorting, ordering, rearranging, etc of a `RelationshipSourceCollection`. - Implement `insert_related` - Implement `insert_children` - Tidy up some docs while I'm here. ## Testing @bjoernp116 set up a unit test, and I added a doc test to `OrderedRelationshipSourceCollection`. --------- Co-authored-by: bjoernp116 <[email protected]> Co-authored-by: Dmytro Banin <[email protected]> Co-authored-by: Talin <[email protected]>
Objective
This PR implements the requested, previously deprecated feature from #17478. Which is concerning the
insert_children
ofEntityWorldMut
. This method inserts children into the tree, at a specific index.Solution
I mostly copied the code from the deprecated crate
bevy_hierarchy
though this is no longer a trait method.This code also used
SmallVec
which has later been switched byVec
. This was easily interchangeable with working code.Testing
I wrote a unit test covering both
EntityWorldMut::add_entities
andEntityWorldMut::insert_entities
, simply calledhierarchy::tests::insert_children
. I was unsure what error handling architecture i should be using, but i think the simplest to debug in this case would just be a panic in the case of an index out of bounds.Showcase
I find this feature pretty self exoplanetary, but the unit test can also bee informing about what this feature implements:
Click to view showcase