Skip to content

Commit 5cbc20f

Browse files
JaySprucemockersf
authored andcommitted
Update relationship commands to use EntityCommands instead of Commands (#18667)
These should use `EntityCommands` so that the entity existence check is hooked up to the default error handler, rather than only panicking.
1 parent 8be0495 commit 5cbc20f

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,11 @@ impl<'a> EntityCommands<'a> {
336336
///
337337
/// See [`add_one_related`](Self::add_one_related) if you want relate only one entity.
338338
pub fn add_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
339-
let id = self.id();
340-
let related = related.to_vec();
341-
self.commands().queue(move |world: &mut World| {
342-
for related in related {
343-
world.entity_mut(related).insert(R::from(id));
344-
}
345-
});
346-
self
339+
let related: Box<[Entity]> = related.into();
340+
341+
self.queue(move |mut entity: EntityWorldMut| {
342+
entity.add_related::<R>(&related);
343+
})
347344
}
348345

349346
/// Relates the given entity to this with the relation `R`.
@@ -355,14 +352,11 @@ impl<'a> EntityCommands<'a> {
355352

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

361-
self.commands().queue(move |world: &mut World| {
362-
world.entity_mut(id).replace_related::<R>(&related);
363-
});
364-
365-
self
357+
self.queue(move |mut entity: EntityWorldMut| {
358+
entity.replace_related::<R>(&related);
359+
})
366360
}
367361

368362
/// Replaces all the related entities with a new set of entities.
@@ -381,30 +375,25 @@ impl<'a> EntityCommands<'a> {
381375
entities_to_relate: &[Entity],
382376
newly_related_entities: &[Entity],
383377
) -> &mut Self {
384-
let id = self.id();
385378
let entities_to_unrelate: Box<[Entity]> = entities_to_unrelate.into();
386379
let entities_to_relate: Box<[Entity]> = entities_to_relate.into();
387380
let newly_related_entities: Box<[Entity]> = newly_related_entities.into();
388381

389-
self.commands().queue(move |world: &mut World| {
390-
world.entity_mut(id).replace_children_with_difference(
382+
self.queue(move |mut entity: EntityWorldMut| {
383+
entity.replace_children_with_difference(
391384
&entities_to_unrelate,
392385
&entities_to_relate,
393386
&newly_related_entities,
394387
);
395-
});
396-
397-
self
388+
})
398389
}
399390

400391
/// Despawns entities that relate to this one via the given [`RelationshipTarget`].
401392
/// This entity will not be despawned.
402393
pub fn despawn_related<S: RelationshipTarget>(&mut self) -> &mut Self {
403-
let id = self.id();
404-
self.commands.queue(move |world: &mut World| {
405-
world.entity_mut(id).despawn_related::<S>();
406-
});
407-
self
394+
self.queue(move |mut entity: EntityWorldMut| {
395+
entity.despawn_related::<S>();
396+
})
408397
}
409398

410399
/// Inserts a component or bundle of components into the entity and all related entities,
@@ -418,11 +407,9 @@ impl<'a> EntityCommands<'a> {
418407
&mut self,
419408
bundle: impl Bundle + Clone,
420409
) -> &mut Self {
421-
let id = self.id();
422-
self.commands.queue(move |world: &mut World| {
423-
world.entity_mut(id).insert_recursive::<S>(bundle);
424-
});
425-
self
410+
self.queue(move |mut entity: EntityWorldMut| {
411+
entity.insert_recursive::<S>(bundle);
412+
})
426413
}
427414

428415
/// Removes a component or bundle of components of type `B` from the entity and all related entities,
@@ -433,11 +420,9 @@ impl<'a> EntityCommands<'a> {
433420
/// This method should only be called on relationships that form a tree-like structure.
434421
/// Any cycles will cause this method to loop infinitely.
435422
pub fn remove_recursive<S: RelationshipTarget, B: Bundle>(&mut self) -> &mut Self {
436-
let id = self.id();
437-
self.commands.queue(move |world: &mut World| {
438-
world.entity_mut(id).remove_recursive::<S, B>();
439-
});
440-
self
423+
self.queue(move |mut entity: EntityWorldMut| {
424+
entity.remove_recursive::<S, B>();
425+
})
441426
}
442427
}
443428

0 commit comments

Comments
 (0)