Skip to content

Required components behave unexpectedly for UI #18779

@Shatur

Description

@Shatur

Bevy version

v0.16.0-rc.3

What you did

use bevy::{input::common_conditions::*, prelude::*};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(ClearColor(Color::srgb(0.9, 0.9, 0.9)))
        .add_systems(Startup, setup)
        .add_systems(Update, show.run_if(input_just_pressed(KeyCode::Space)))
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);

    commands.spawn((
        Node {
            width: Val::Percent(100.0),
            height: Val::Percent(100.0),
            ..Default::default()
        },
        // Yes, all this hierarchy nodes are required to trigger the issue.
        children![(Node::default(), children![Node::default()])],
    ));
}

fn show(mut commands: Commands, root_entity: Single<Entity, (With<Node>, Without<ChildOf>)>) {
    info!("showing dialog");

    commands.spawn((
        ChildOf {
            parent: *root_entity,
        },
        Node {
            width: Val::Percent(100.0),
            height: Val::Percent(100.0),
            align_items: AlignItems::Center,
            justify_content: JustifyContent::Center,
            ..Default::default()
        },
        BackgroundColor(Color::BLACK),
        // Uncommend this `Dialog` component and the text won't be displayed.
        // Dialog,
        children![(
            Node::default(),
            children![Text::new("This text should be displayed")]
        )],
    ));
}

#[derive(Component, Default)]
#[require(
    Node {
        width: Val::Percent(100.0),
        height: Val::Percent(100.0),
        align_items: AlignItems::Center,
        justify_content: JustifyContent::Center,
        ..Default::default()
    },
    BackgroundColor(Color::BLACK),
)]
struct Dialog;

What went wrong

With very specific hierarchy setups (see code comments), using required components sometimes behaves differently from inserting the necessary components directly. If you uncomment Dialog insertion, the text won't be displayed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsA-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behaviorS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions