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

Remove child components during on_remove #6

Open
matthunz opened this issue Nov 5, 2024 · 0 comments · May be fixed by #7
Open

Remove child components during on_remove #6

matthunz opened this issue Nov 5, 2024 · 0 comments · May be fixed by #7

Comments

@matthunz
Copy link

matthunz commented Nov 5, 2024

Currently WithChild and WithChildren only register an on_add hook. This is incredibly convenient for bundling up hierarchies, but feels like it's missing a way to automatically remove these child components.

An alternative design might be to also register child removal during on_remove. This may add overhead while despawning a hierarchy, but I think for most cases it would be preferred to remove the spawned child components when WithChild or WithChildren is removed.

let mut world = World::default();

let parent = world.spawn(WithChild::new((A, B(3)))).id();
// FIXME: this should not be needed!
world.flush();

assert!(!world.entity(parent).contains::<A>());
assert!(!world.entity(parent).contains::<B>());

let children = world.get::<Children>(parent).unwrap();
assert_eq!(children.len(), 1);

let child_entity = children[0];
assert_eq!(world.get::<A>(child_entity), Some(&A));
assert_eq!(world.get::<B>(child_entity), Some(&B(3)));

world.entity_mut(parent).remove::<WithChild<(A, B)>>();
// FIXME: this should not be needed!
world.flush();

assert!(world.get::<A>(child_entity).is_none());
assert!(world.get::<B>(child_entity).is_none());
@matthunz matthunz linked a pull request Nov 5, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant