Skip to content
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

fix panic when moving child #8346

Merged
merged 6 commits into from
Apr 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}
}

Expand Down Expand Up @@ -653,6 +656,23 @@ mod tests {
);
}

// regression test for https://github.com/bevyengine/bevy/pull/8346
#[test]
james7132 marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down