-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Add insert_children
and push_children
to EntityMut
#1728
Conversation
|
||
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.
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.
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 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.
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).
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 <[email protected]>
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 sincePreviousParent
has no public constructor.This PR adds two methods to set entities as children of an
EntityMut
:insert_children
andpush_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
Parent
andChildren
components are defined inbevy_transform
which depends onbevy_ecs
, whileEntityMut
is defined inbevy_ecs
, so the methods are added to theBuildWorldChildren
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.