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).
This commit is contained in:
JaySpruce 2025-04-02 12:31:29 -05:00 committed by GitHub
parent 86fa2ac570
commit 6fc31bc623
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View File

@ -361,6 +361,12 @@ impl<'a> EntityCommands<'a> {
self.add_related::<ChildOf>(children)
}
/// Insert children at specific index.
/// See also [`insert_related`](Self::insert_related).
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
self.insert_related::<ChildOf>(index, children)
}
/// Adds the given child to this entity
pub fn add_child(&mut self, child: Entity) -> &mut Self {
self.add_related::<ChildOf>(&[child])

View File

@ -343,6 +343,23 @@ impl<'a> EntityCommands<'a> {
})
}
/// Relates the given entities to this entity with the relation `R`, starting at this particular index.
///
/// If the `related` has duplicates, a related entity will take the index of its last occurrence in `related`.
/// If the indices go out of bounds, they will be clamped into bounds.
/// This will not re-order existing related entities unless they are in `related`.
pub fn insert_related<R: Relationship>(&mut self, index: usize, related: &[Entity]) -> &mut Self
where
<R::RelationshipTarget as RelationshipTarget>::Collection:
OrderedRelationshipSourceCollection,
{
let related: Box<[Entity]> = related.into();
self.queue(move |mut entity: EntityWorldMut| {
entity.insert_related::<R>(index, &related);
})
}
/// Relates the given entity to this with the relation `R`.
///
/// See [`add_related`](Self::add_related) if you want to relate more than one entity.