Skip to content

[Merged by Bors] - Add insert_children and push_children to EntityMut#1728

Closed
Davier wants to merge 2 commits intobevyengine:mainfrom
Davier:entitymut_children
Closed

[Merged by Bors] - Add insert_children and push_children to EntityMut#1728
Davier wants to merge 2 commits intobevyengine:mainfrom
Davier:entitymut_children

Conversation

@Davier
Copy link
Contributor

@Davier Davier commented Mar 22, 2021

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.

@Ratysz Ratysz added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Mar 22, 2021

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.

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

@cart cart Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@cart
Copy link
Member

cart commented Mar 26, 2021

bors r+

bors bot pushed a commit that referenced this pull request Mar 26, 2021
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>
@bors
Copy link
Contributor

bors bot commented Mar 26, 2021

@bors bors bot changed the title Add insert_children and push_children to EntityMut [Merged by Bors] - Add insert_children and push_children to EntityMut Mar 26, 2021
@bors bors bot closed this Mar 26, 2021
@Davier Davier deleted the entitymut_children branch March 27, 2021 04:39
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants