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

Insert children #17558

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions crates/bevy_ecs/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,33 @@ impl<'w> EntityWorldMut<'w> {
self.add_related::<ChildOf>(children)
}

/// Insert children at specific index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the entity is already a child? As a user, I'd like to know whether that is okay ok or not. For example, does doing so reorder it?

pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot insert entity as a child of itself.");
}
if let Some(mut children_component) = self.get_mut::<Children>() {
children_component
.0
.retain(|value| !children.contains(value));
if index >= children_component.len() {
panic!(
"Index {} out of bounds! There are only {} children!",
index,
children.len()
);
}
children_component.0.reserve(children.len());
let mut v = children_component.0.split_off(index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not update the other side of the relationship (ChildOf), invalidating the assumptions of the system as a whole.

children_component.0.extend_from_slice(children);
children_component.0.append(&mut v);
} else {
self.insert(Children(children.to_vec()));
}
self
}

/// Adds the given child to this entity
pub fn add_child(&mut self, child: Entity) -> &mut Self {
self.add_related::<ChildOf>(&[child])
Expand Down Expand Up @@ -416,6 +443,35 @@ mod tests {
);
}

#[test]
fn insert_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 entity_world_mut = world.spawn_empty();

let first_children = entity_world_mut.add_children(&[child1, child2]);

let root = first_children.insert_children(1, &[child3, child4]).id();

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

#[test]
fn self_parenting_invalid() {
let mut world = World::new();
Expand Down
Loading