Skip to content

TextBundle does not recognize it is a child of a NodeBundle #6959

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
campbellcole opened this issue Dec 15, 2022 · 6 comments
Closed

TextBundle does not recognize it is a child of a NodeBundle #6959

campbellcole opened this issue Dec 15, 2022 · 6 comments
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@campbellcole
Copy link

campbellcole commented Dec 15, 2022

Bevy version

0.9.1

What you did

Spawning the following two entities causes the console to be flooded with warning messages that appear to be incorrect:

commands
    .spawn((
        NodeBundle {
            style: Style {
                size: Size::AUTO,
                margin: UiRect::all(Val::Auto),
                ..default()
            },
            ..default()
        },
        MusicDropUI,
    ))
    .with_children(|parent| {
        parent.spawn(
            TextBundle::from_section(
                "Drag and drop a music file to begin",
                TextStyle {
                    font: font_handles.fonts.get(&TryptFont::Regular).unwrap().clone(),
                    font_size: 32.0,
                    color: Color::WHITE,
                },
            )
            .with_style(Style {
                align_self: AlignSelf::Center,
                ..default()
            }),
        );
    });

Some context, as I imagine this may not be reproducible from just this code: This setup function runs immediately after despawning every entity in the world, so it should be a fresh start (and using bevy_inspector_egui correctly demonstrates this). The only two existing entities in the world after this command are a Camera2DBundle and the root UI node, with it's TextBundle child:

image

What went wrong

The console gets flooded with warnings stating that a style component has a parent which is not style, but this is not the case. The only entity with a parent has a ui node as it's parent.

Additional information

This message only appears after despawning a previous UI hierarchy and then spawning this one. I cannot figure out how to make a minimum reproducible example because I have no idea what is causing this to happen.

@campbellcole campbellcole added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Dec 15, 2022
@nicopap
Copy link
Contributor

nicopap commented Dec 15, 2022

I can't reproduce with the minimal example you provided. Is there other code than what you posted? Could I take a look at it? It's fine if it's not minimal, as long as I can reproduce the error message

@nicopap nicopap added A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Dec 15, 2022
@campbellcole
Copy link
Author

campbellcole commented Dec 18, 2022

I just figured out what the problem was. I was using a helper system that I labeled despawn_recursive, but somehow I managed to use the despawn function instead of despawn_recursive. This was creating orphan UI entities that did not appear in the inspector GUI.

To show how this happened, first we have the function responsible for registering these systems with the app:

app.add_enter_system_set(
    AppState::WaitingForMusic,
    SystemSet::new()
        .with_system(utils::despawn_recursive::<MenuComponent>)
        .with_system(setup_dnd_ui),
)
.add_system(wait_for_drop.run_in_state(AppState::WaitingForMusic));

You can see my helper system despawn_recursive<C: Component> being used here, however the definition was (erroneously) as follows:

pub fn despawn_recursive<T: Component>(mut commands: Commands, query: Query<Entity, With<T>>) {
    for entity in query.iter() {
        commands.entity(entity).despawn();
    }
}

I must use despawn_recursive because of the way I set up my UI components. I only gave the root UI component the MenuComponent marker component because I was supposed to be using despawn_recursive:

commands
    .spawn((
        NodeBundle {
            style: Style {
                size: Size::new(Val::Px(450.0), Val::Px(300.0)),
                flex_direction: FlexDirection::Column,
                justify_content: JustifyContent::Center,
                margin: UiRect::all(Val::Auto),
                ..default()
            },
            ..default()
        },
        MenuComponent,
    ))
    .with_children(|parent| {
        ...
    });

Changing this to commands.entity(entity).despawn_recursive() solved the problem. I don't know how I messed this up, but I'm glad I figured it out. I do wonder though, and perhaps this is out of scope for this crate, but is there any defined behavior for orphaning an entity? It seems like the child entity still believes it has a parent, but since the parent entity was deleted it finds nothing, including not finding a Node component. My first guess would be that the child becomes the new parent, but that doesn't seem to be happening. Thanks for the quick reply btw.

@nicopap
Copy link
Contributor

nicopap commented Dec 18, 2022

It is a bug to have an entity with a Parent(foo) component when foo doesn't have a child. I think it is also a bug to have a Parent(v) component where v is an empty list. If you can reproduce this, could you share the code to reproduce it? Then I could open a new issue.

@mockersf
Copy link
Member

despawn has no knowledge of hierarchy and so will not update Parent or Children components by design.

@Weibye
Copy link
Contributor

Weibye commented Jan 12, 2023

Should this be closed as "working as intended" or renamed to "Bug: Despawn does not update Parent / Children components"?

@rparrett
Copy link
Contributor

At the very least this could be closed as a duplicate of #5584.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

5 participants