Skip to content

Commit deba691

Browse files
JaySprucemockersf
authored andcommitted
Implement insert_children for EntityCommands (#18675)
Extension of #18409. I was updating a migration guide for hierarchy commands and realized `insert_children` wasn't added to `EntityCommands`, only `EntityWorldMut`. This adds that and `insert_related` (basically just some copy-and-pasting).
1 parent d5d799c commit deba691

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

crates/bevy_ecs/src/hierarchy.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ impl<'a> EntityCommands<'a> {
361361
self.add_related::<ChildOf>(children)
362362
}
363363

364+
/// Insert children at specific index.
365+
/// See also [`insert_related`](Self::insert_related).
366+
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
367+
self.insert_related::<ChildOf>(index, children)
368+
}
369+
364370
/// Adds the given child to this entity
365371
pub fn add_child(&mut self, child: Entity) -> &mut Self {
366372
self.add_related::<ChildOf>(&[child])

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,23 @@ impl<'a> EntityCommands<'a> {
343343
})
344344
}
345345

346+
/// Relates the given entities to this entity with the relation `R`, starting at this particular index.
347+
///
348+
/// If the `related` has duplicates, a related entity will take the index of its last occurrence in `related`.
349+
/// If the indices go out of bounds, they will be clamped into bounds.
350+
/// This will not re-order existing related entities unless they are in `related`.
351+
pub fn insert_related<R: Relationship>(&mut self, index: usize, related: &[Entity]) -> &mut Self
352+
where
353+
<R::RelationshipTarget as RelationshipTarget>::Collection:
354+
OrderedRelationshipSourceCollection,
355+
{
356+
let related: Box<[Entity]> = related.into();
357+
358+
self.queue(move |mut entity: EntityWorldMut| {
359+
entity.insert_related::<R>(index, &related);
360+
})
361+
}
362+
346363
/// Relates the given entity to this with the relation `R`.
347364
///
348365
/// See [`add_related`](Self::add_related) if you want to relate more than one entity.

0 commit comments

Comments
 (0)