Simplified on_replace and on_despawn relationship hooks. (#19378)
Fixes #18364. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
3d7486b019
commit
415f6d8ca7
@ -14,7 +14,6 @@ use crate::{
|
|||||||
component::{Component, HookContext, Mutable},
|
component::{Component, HookContext, Mutable},
|
||||||
entity::{ComponentCloneCtx, Entity, SourceComponent},
|
entity::{ComponentCloneCtx, Entity, SourceComponent},
|
||||||
error::{ignore, CommandWithEntity, HandleError},
|
error::{ignore, CommandWithEntity, HandleError},
|
||||||
system::entity_command::{self},
|
|
||||||
world::{DeferredWorld, EntityWorldMut},
|
world::{DeferredWorld, EntityWorldMut},
|
||||||
};
|
};
|
||||||
use log::warn;
|
use log::warn;
|
||||||
@ -223,50 +222,24 @@ pub trait RelationshipTarget: Component<Mutability = Mutable> + Sized {
|
|||||||
|
|
||||||
/// The `on_replace` component hook that maintains the [`Relationship`] / [`RelationshipTarget`] connection.
|
/// The `on_replace` component hook that maintains the [`Relationship`] / [`RelationshipTarget`] connection.
|
||||||
// note: think of this as "on_drop"
|
// 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 (entities, mut commands) = world.entities_and_commands();
|
||||||
let relationship_target = entities.get(entity).unwrap().get::<Self>().unwrap();
|
let relationship_target = entities.get(entity).unwrap().get::<Self>().unwrap();
|
||||||
for source_entity in relationship_target.iter() {
|
for source_entity in relationship_target.iter() {
|
||||||
if entities.get(source_entity).is_ok() {
|
commands
|
||||||
commands.queue(
|
.entity(source_entity)
|
||||||
entity_command::remove::<Self::Relationship>()
|
.remove::<Self::Relationship>();
|
||||||
.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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The `on_despawn` component hook that despawns entities stored in an entity's [`RelationshipTarget`] when
|
/// The `on_despawn` component hook that despawns entities stored in an entity's [`RelationshipTarget`] when
|
||||||
/// that entity is despawned.
|
/// that entity is despawned.
|
||||||
// note: think of this as "on_drop"
|
// 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 (entities, mut commands) = world.entities_and_commands();
|
||||||
let relationship_target = entities.get(entity).unwrap().get::<Self>().unwrap();
|
let relationship_target = entities.get(entity).unwrap().get::<Self>().unwrap();
|
||||||
for source_entity in relationship_target.iter() {
|
for source_entity in relationship_target.iter() {
|
||||||
if entities.get(source_entity).is_ok() {
|
commands.entity(source_entity).despawn();
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user