Skip to content

Fix custom relations panics with parent/child relations #19341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 71 additions & 10 deletions crates/bevy_ecs/src/relationship/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,21 @@ pub trait Relationship: Component + Sized {
{
relationship_target.collection_mut_risky().remove(entity);
if relationship_target.len() == 0 {
if let Ok(mut entity) = world.commands().get_entity(target_entity) {
let command = |mut entity: EntityWorldMut| {
// this "remove" operation must check emptiness because in the event that an identical
// relationship is inserted on top, this despawn would result in the removal of that identical
// relationship ... not what we want!
entity.queue(|mut entity: EntityWorldMut| {
if entity
.get::<Self::RelationshipTarget>()
.is_some_and(RelationshipTarget::is_empty)
{
entity.remove::<Self::RelationshipTarget>();
}
});
}
if entity
.get::<Self::RelationshipTarget>()
.is_some_and(RelationshipTarget::is_empty)
{
entity.remove::<Self::RelationshipTarget>();
}
};

world
.commands()
.queue(command.with_entity(target_entity).handle_error_with(ignore));
}
}
}
Expand Down Expand Up @@ -424,4 +426,63 @@ mod tests {

// No assert necessary, looking to make sure compilation works with the macros
}

#[test]
fn parent_child_relationship_with_custom_relationship() {
use crate::prelude::ChildOf;

#[derive(Component)]
#[relationship(relationship_target = RelTarget)]
struct Rel(Entity);

#[derive(Component)]
#[relationship_target(relationship = Rel)]
struct RelTarget(Entity);

let mut world = World::new();

// Rel on Parent
// Despawn Parent
let mut commands = world.commands();
let child = commands.spawn_empty().id();
let parent = commands.spawn(Rel(child)).add_child(child).id();
commands.entity(parent).despawn();
world.flush();

assert!(world.get_entity(child).is_err());
assert!(world.get_entity(parent).is_err());

// Rel on Parent
// Despawn Child
let mut commands = world.commands();
let child = commands.spawn_empty().id();
let parent = commands.spawn(Rel(child)).add_child(child).id();
commands.entity(child).despawn();
world.flush();

assert!(world.get_entity(child).is_err());
assert!(!world.entity(parent).contains::<Rel>());

// Rel on Child
// Despawn Parent
let mut commands = world.commands();
let parent = commands.spawn_empty().id();
let child = commands.spawn((ChildOf(parent), Rel(parent))).id();
commands.entity(parent).despawn();
world.flush();

assert!(world.get_entity(child).is_err());
assert!(world.get_entity(parent).is_err());

// Rel on Child
// Despawn Child
let mut commands = world.commands();
let parent = commands.spawn_empty().id();
let child = commands.spawn((ChildOf(parent), Rel(parent))).id();
commands.entity(child).despawn();
world.flush();

assert!(world.get_entity(child).is_err());
assert!(!world.entity(parent).contains::<RelTarget>());
}
}