fix panic when moving child (#8346)

# Objective
When changing an Entity's `Parent` to a new one from an old `Parent`
that doesn't exist, Bevy panics. Fixes #8337.

## Solution

Use `get_entity_mut` instead of `entity_mut` in `remove_from_children`.

---------

Co-authored-by: James Liu <contact@jamessliu.com>
This commit is contained in:
Bruce Reif (Buswolley) 2023-04-17 11:03:47 -07:00 committed by GitHub
parent 8ed7723823
commit 7604464438
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,12 +41,15 @@ fn update_parent(world: &mut World, child: Entity, new_parent: Entity) -> Option
///
/// Removes the [`Children`] component from the parent if it's empty.
fn remove_from_children(world: &mut World, parent: Entity, child: Entity) {
let mut parent = world.entity_mut(parent);
if let Some(mut children) = parent.get_mut::<Children>() {
children.0.retain(|x| *x != child);
if children.is_empty() {
parent.remove::<Children>();
}
let Some(mut parent) = world.get_entity_mut(parent) else {
return;
};
let Some(mut children) = parent.get_mut::<Children>() else {
return;
};
children.0.retain(|x| *x != child);
if children.is_empty() {
parent.remove::<Children>();
}
}
@ -653,6 +656,23 @@ mod tests {
);
}
// regression test for https://github.com/bevyengine/bevy/pull/8346
#[test]
fn set_parent_of_orphan() {
let world = &mut World::new();
let [a, b, c] = std::array::from_fn(|_| world.spawn_empty().id());
world.entity_mut(a).set_parent(b);
assert_parent(world, a, Some(b));
assert_children(world, b, Some(&[a]));
world.entity_mut(b).despawn();
world.entity_mut(a).set_parent(c);
assert_parent(world, a, Some(c));
assert_children(world, c, Some(&[a]));
}
#[test]
fn remove_parent() {
let world = &mut World::new();