Skip to content

Commit 7aaf440

Browse files
authored
Fix push_children inserting a Children component even when no children are supplied (#14109)
# Objective The Bevy API around manipulating hierarchies removes `Children` if the operation results in an entity having no children. This means that `Children` is guaranteed to hold actual children. However, the following code unexpectedly inserts empty `Children`: ```rust commands.entity(entity).with_children(|_| {}); ``` This was discovered by @Jondolf: https://discord.com/channels/691052431525675048/1124043933886976171/1257660865625325800 ## Solution - `with_children` is now a noop when no children were passed ## Testing - Added a regression test
1 parent 5876352 commit 7aaf440

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

crates/bevy_hierarchy/src/child_builder.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ impl BuildChildren for EntityWorldMut<'_> {
584584
}
585585

586586
fn push_children(&mut self, children: &[Entity]) -> &mut Self {
587+
if children.is_empty() {
588+
return self;
589+
}
590+
587591
let parent = self.id();
588592
if children.contains(&parent) {
589593
panic!("Cannot push entity as a child of itself.");
@@ -1214,4 +1218,14 @@ mod tests {
12141218
let children = query.get(&world, parent).unwrap();
12151219
assert_eq!(**children, [child]);
12161220
}
1221+
1222+
#[test]
1223+
fn push_children_does_not_insert_empty_children() {
1224+
let mut world = World::new();
1225+
let parent = world.spawn_empty().push_children(&[]).id();
1226+
1227+
let mut query = world.query::<&Children>();
1228+
let children = query.get(&world, parent);
1229+
assert!(children.is_err());
1230+
}
12171231
}

0 commit comments

Comments
 (0)