[Merged by Bors] - Add insert_children and push_children to EntityMut#1728
[Merged by Bors] - Add insert_children and push_children to EntityMut#1728Davier wants to merge 2 commits intobevyengine:mainfrom
insert_children and push_children to EntityMut#1728Conversation
|
|
||
| if let Some(mut children_component) = world.get_mut::<Children>(parent) { | ||
| children_component.0.insert_from_slice(index, children); | ||
| } else { |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| if let Some(mut children_component) = world.get_mut::<Children>(parent) { | ||
| children_component.0.extend(children.iter().cloned()); | ||
| } else { | ||
| world.entity_mut(parent).insert(Children::with(children)); |
There was a problem hiding this comment.
We can optimize this by using self (which is world.entity_mut(parent)). Additionally, we haven't updated the entity location, so theres no need to call self.update_location() here.
I'll just push the changes so we can get this merged asap.
There was a problem hiding this comment.
I guess I should say: "we haven't modified the entity location using the unsafe world reference". Using self.insert will handle the location stuff for us.
| let parent = self | ||
| .current_entity | ||
| .expect("Cannot add children without a parent. Try creating an entity first."); | ||
| self.parent_entities.push(parent); |
There was a problem hiding this comment.
We aren't creating a "nested WorldChildBuilder context" here like we do in with_children, so theres no need to push the parent entity (and in fact, that will break things).
|
bors r+ |
The only API to add a parent/child relationship between existing entities is through commands, there is no easy way to do it from `World`. Manually inserting the components is not completely possible since `PreviousParent` has no public constructor. This PR adds two methods to set entities as children of an `EntityMut`: `insert_children` and `push_children`. ~~The API is similar to the one on `Commands`, except that the parent is the `EntityMut`.~~ The API is the same as in #1703. However, the `Parent` and `Children` components are defined in `bevy_transform` which depends on `bevy_ecs`, while `EntityMut` is defined in `bevy_ecs`, so the methods are added to the `BuildWorldChildren` trait instead. If #1545 is merged this should be fixed too. I'm aware cart was experimenting with entity hierarchies, but unless it's a coming soon this PR would be useful to have meanwhile. Co-authored-by: Carter Anderson <mcanders1@gmail.com>
|
Pull request successfully merged into main. Build succeeded: |
insert_children and push_children to EntityMutinsert_children and push_children to EntityMut
The only API to add a parent/child relationship between existing entities is through commands, there is no easy way to do it from
World. Manually inserting the components is not completely possible sincePreviousParenthas no public constructor.This PR adds two methods to set entities as children of an
EntityMut:insert_childrenandpush_children.The API is similar to the one onThe API is the same as in #1703.Commands, except that the parent is theEntityMut.However, the
ParentandChildrencomponents are defined inbevy_transformwhich depends onbevy_ecs, whileEntityMutis defined inbevy_ecs, so the methods are added to theBuildWorldChildrentrait instead.If #1545 is merged this should be fixed too.
I'm aware cart was experimenting with entity hierarchies, but unless it's a coming soon this PR would be useful to have meanwhile.