Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions crates/bevy_ecs/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ impl<'w> EntityWorldMut<'w> {
self.add_related::<ChildOf>(&[child])
}

/// Removes the relationship between this entity and the given entities.
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
self.remove_related::<ChildOf>(children)
}

/// Replaces all the related children with a new set of children.
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
self.replace_related::<ChildOf>(children)
Expand Down Expand Up @@ -372,6 +377,11 @@ impl<'a> EntityCommands<'a> {
self.add_related::<ChildOf>(&[child])
}

/// Removes the relationship between this entity and the given entities.
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
self.remove_related::<ChildOf>(children)
}

/// Replaces the children on this entity with a new list of children.
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
self.replace_related::<ChildOf>(children)
Expand Down Expand Up @@ -643,6 +653,26 @@ mod tests {
);
}

#[test]
fn remove_children() {
let mut world = World::new();
let child1 = world.spawn_empty().id();
let child2 = world.spawn_empty().id();
let child3 = world.spawn_empty().id();
let child4 = world.spawn_empty().id();

let mut root = world.spawn_empty();
root.add_children(&[child1, child2, child3, child4]);
root.remove_children(&[child2, child3]);
let root = root.id();

let hierarchy = get_hierarchy(&world, root);
assert_eq!(
hierarchy,
Node::new_with(root, vec![Node::new(child1), Node::new(child4)])
);
}

#[test]
fn self_parenting_invalid() {
let mut world = World::new();
Expand Down
26 changes: 26 additions & 0 deletions crates/bevy_ecs/src/relationship/related_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ impl<'w> EntityWorldMut<'w> {
self
}

/// Removes the relation `R` between this entity and the given entities.
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
let id = self.id();
self.world_scope(|world| {
for related in related {
if world
.get::<R>(*related)
.is_some_and(|relationship| relationship.get() == id)
{
world.entity_mut(*related).remove::<R>();
}
}
});

self
}

/// Replaces all the related entities with a new set of entities.
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
type Collection<R> =
Expand Down Expand Up @@ -383,6 +400,15 @@ impl<'a> EntityCommands<'a> {
self.add_related::<R>(&[entity])
}

/// Removes the relation `R` between this entity and the given entities.
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
let related: Box<[Entity]> = related.into();

self.queue(move |mut entity: EntityWorldMut| {
entity.remove_related::<R>(&related);
})
}

/// Replaces all the related entities with the given set of new related entities.
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
let related: Box<[Entity]> = related.into();
Expand Down