Skip to content

Having a custom relation and a parent/child relation at the same time causes panics on despawn #18905

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

Closed
OwlyCode opened this issue Apr 23, 2025 · 1 comment · Fixed by #19341
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior
Milestone

Comments

@OwlyCode
Copy link
Contributor

Bevy version

0.16-rc5

What you did

I tried to add a relation between entities that are also parent / child of each other. My goal was to remove the need of iterating children looking for marker components.

// The relationship declaration
#[derive(Component)]
#[relationship(relationship_target = HairOf)]
pub struct HasHair(Entity);

impl HasHair {
    pub fn new(hair: Entity) -> Self {
        HasHair(hair)
    }
}

#[derive(Component)]
#[relationship_target(relationship = HasHair)]
pub struct HairOf(Entity);

// Setup
let hair_entity = commands.spawn(Hair(character.get_hair())).id();

commands
    .entity(character_entity)
    .insert((
        Name::new(character.get_name()),
        HasHair::new(hair_entity),
    ))
    .add_child(hair_entity);



// later in a system
commands.despawn(character_entity);

What went wrong

It panicked with the following:

Encountered an error in command `<<models::greenfeet::body::HasHair as bevy_ecs::relationship::Relationship>::on_replace::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: The entity with ID 97670v1 was despawned by .cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.16.0-rc.5\src\relationship\mod.rs:255:21
@OwlyCode OwlyCode added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Apr 23, 2025
@OwlyCode OwlyCode changed the title Conflicting relations Having a custom relation and a parent/child relation at the same time causes panics on despawn Apr 23, 2025
@janhohenheim janhohenheim added this to the 0.16.1 milestone Apr 23, 2025
@janhohenheim
Copy link
Member

Added it to 0.16.1 since this seems like a very common use-case

@janhohenheim janhohenheim added A-ECS Entities, components, systems, and events and removed S-Needs-Triage This issue needs to be labelled labels Apr 23, 2025
@alice-i-cecile alice-i-cecile added the S-Needs-Investigation This issue requires detective work to figure out what's going wrong label Apr 25, 2025
@UkoeHB UkoeHB removed the S-Needs-Investigation This issue requires detective work to figure out what's going wrong label May 23, 2025
github-merge-queue bot pushed a commit that referenced this issue May 27, 2025
# Objective

Fixes #18905

## Solution

`world.commands().entity(target_entity).queue(command)` calls
`commands.with_entity` without an error handler, instead queue on
`Commands` with an error handler

## Testing

Added unit test

Co-authored-by: Heart <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants