Skip to content

commands despawn does not update childrens parent #5584

@PhaestusFox

Description

@PhaestusFox

Bevy version

0.8

What you did

I making a video about the new changes to bevy's hierarchy and I found that if you despawn an entity that has children, said children will keep the now non-existent entity as their parent.

#[derive(Component)]
struct Test([Entity; 3]);

fn startup(mut commands: Commands) {
 let c0 = commands.spawn().id();
 let c1 = commands.spawn().add_child(c0).id();
 let c2 = commands.spawn().add_child(c0).id();
 commands.insert_resource(Test([c0, c1, c2]));
 commands.entity(c2).despawn();
}

fn run(test: Res<Test>,
    child: Query<&Children>,
    parent: Query<&Parent>,
    input: Res<Input<KeyCode>>,
) {
    if input.just_pressed(KeyCode::Space) {
        println!("C0:{:?}, C1:{:?}, C2:{:?}", test.0[0],test.0[1],test.0[2]);
        println!("C0 Parent is {}", match parent.get(test.0[0]) {
            Ok(e) => {
            if e.get() == test.0[1] {
                "C1".to_string()
            } else if e.get() == test.0[2] {
                "C2".to_string()
            } else {
                format!("{:?}", e)
            }
            },
            Err(e)=> {format!("{:?}", e)},
        } );
        println!("C1 Children are {:?}", child.get(test.0[1]));
        println!("C2 Children are {:?}", child.get(test.0[2]));
    }
}

What went wrong

  • what were you expecting?
    console to read
C0:1v0, C1:2v0, C2:3v0
C0 Parent is QueryDoesNotMatch(1v0)
C1 Children are Err(QueryDoesNotMatch(2v0))
C2 Children are Err(NoSuchEntity(3v0))
  • what actually happened?
    console to reads
C0:1v0, C1:2v0, C2:3v0
C0 Parent is C2
C1 Children are Err(QueryDoesNotMatch(2v0))
C2 Children are Err(NoSuchEntity(3v0))

I believe this should be classified as a bug because it would be unexpected for children to reference a parent that has been despawned, this also breads the ease of finding root nodes as these nodes are now free floating.

How to fix

I don't know whether
they should just be cut free to become roots themself -- easier
or if
they should be grafted up a parent if there is one to attach to if not then made free -- harder/slower

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-BugAn unexpected or incorrect behaviorD-ComplexQuite challenging from either a design or technical perspective. Ask for help!S-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions