Skip to content

Commit

Permalink
Fix with_child not inserting Parent component (#15009)
Browse files Browse the repository at this point in the history
# Objective

The `Parent` component holds a reference to the parent entity of the
entity it is inserted onto. The `with_child` function erroneously
forgets to insert this component onto the child entity that it spawns,
causing buggy behaviour when the function is used instead of the other
child-spawning functions.

## Solution

Ensure `with_child` inserts the `Parent` component, the same as all the
other child-spawning functions.

## Testing

Checked before/after with a bevy_ui layout where this patch fixed buggy
behaviour I was seeing in parent/child UI nodes.
  • Loading branch information
BigWingBeat authored Sep 2, 2024
1 parent 3fc02cb commit 61f9f8c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@ impl BuildChildren for EntityWorldMut<'_> {
}

fn with_child<B: Bundle>(&mut self, bundle: B) -> &mut Self {
let child = self.world_scope(|world| world.spawn(bundle).id());
let parent = self.id();
let child = self.world_scope(|world| world.spawn((bundle, Parent(parent))).id());
if let Some(mut children_component) = self.get_mut::<Children>() {
children_component.0.retain(|value| child != *value);
children_component.0.push(child);
Expand Down

0 comments on commit 61f9f8c

Please sign in to comment.