From 415f6d8ca7800ec92da84ae16b8049c1ef3f0f9d Mon Sep 17 00:00:00 2001 From: AlephCubed <76791009+AlephCubed@users.noreply.github.com> Date: Mon, 2 Jun 2025 15:15:18 -0700 Subject: [PATCH] Simplified `on_replace` and `on_despawn` relationship hooks. (#19378) Fixes #18364. --------- Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/relationship/mod.rs | 39 ++++--------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/crates/bevy_ecs/src/relationship/mod.rs b/crates/bevy_ecs/src/relationship/mod.rs index 3522118fbc..9ec67ce36a 100644 --- a/crates/bevy_ecs/src/relationship/mod.rs +++ b/crates/bevy_ecs/src/relationship/mod.rs @@ -14,7 +14,6 @@ use crate::{ component::{Component, HookContext, Mutable}, entity::{ComponentCloneCtx, Entity, SourceComponent}, error::{ignore, CommandWithEntity, HandleError}, - system::entity_command::{self}, world::{DeferredWorld, EntityWorldMut}, }; use log::warn; @@ -223,50 +222,24 @@ pub trait RelationshipTarget: Component + Sized { /// The `on_replace` component hook that maintains the [`Relationship`] / [`RelationshipTarget`] connection. // note: think of this as "on_drop" - fn on_replace(mut world: DeferredWorld, HookContext { entity, caller, .. }: HookContext) { + fn on_replace(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) { let (entities, mut commands) = world.entities_and_commands(); let relationship_target = entities.get(entity).unwrap().get::().unwrap(); for source_entity in relationship_target.iter() { - if entities.get(source_entity).is_ok() { - commands.queue( - entity_command::remove::() - .with_entity(source_entity) - .handle_error_with(ignore), - ); - } else { - warn!( - "{}Tried to despawn non-existent entity {}", - caller - .map(|location| format!("{location}: ")) - .unwrap_or_default(), - source_entity - ); - } + commands + .entity(source_entity) + .remove::(); } } /// The `on_despawn` component hook that despawns entities stored in an entity's [`RelationshipTarget`] when /// that entity is despawned. // note: think of this as "on_drop" - fn on_despawn(mut world: DeferredWorld, HookContext { entity, caller, .. }: HookContext) { + fn on_despawn(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) { let (entities, mut commands) = world.entities_and_commands(); let relationship_target = entities.get(entity).unwrap().get::().unwrap(); for source_entity in relationship_target.iter() { - if entities.get(source_entity).is_ok() { - commands.queue( - entity_command::despawn() - .with_entity(source_entity) - .handle_error_with(ignore), - ); - } else { - warn!( - "{}Tried to despawn non-existent entity {}", - caller - .map(|location| format!("{location}: ")) - .unwrap_or_default(), - source_entity - ); - } + commands.entity(source_entity).despawn(); } }