mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
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
This commit is contained in:
parent
5876352206
commit
7aaf440fbf
1 changed files with 14 additions and 0 deletions
|
@ -584,6 +584,10 @@ impl BuildChildren for EntityWorldMut<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_children(&mut self, children: &[Entity]) -> &mut Self {
|
fn push_children(&mut self, children: &[Entity]) -> &mut Self {
|
||||||
|
if children.is_empty() {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
let parent = self.id();
|
let parent = self.id();
|
||||||
if children.contains(&parent) {
|
if children.contains(&parent) {
|
||||||
panic!("Cannot push entity as a child of itself.");
|
panic!("Cannot push entity as a child of itself.");
|
||||||
|
@ -1214,4 +1218,14 @@ mod tests {
|
||||||
let children = query.get(&world, parent).unwrap();
|
let children = query.get(&world, parent).unwrap();
|
||||||
assert_eq!(**children, [child]);
|
assert_eq!(**children, [child]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn push_children_does_not_insert_empty_children() {
|
||||||
|
let mut world = World::new();
|
||||||
|
let parent = world.spawn_empty().push_children(&[]).id();
|
||||||
|
|
||||||
|
let mut query = world.query::<&Children>();
|
||||||
|
let children = query.get(&world, parent);
|
||||||
|
assert!(children.is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue