Skip to content

Hiding parent entity does not hide children #838

Closed

Description

@AlexCMueller

When I first tested this, I expected hiding a parent entity to also hide its children. This would be a useful behavior: an example would be making a rudimentary state system by having each state be represented by a root entity, and hiding the root entity of inactive states so the entire states aren't visible.

Below is a minimal example of a child still being visible after the parent is hidden, using bevy 0.3.0.

use bevy::prelude::*;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins) 
        .add_startup_system(setup.system())
        .run();
}

fn setup(
    mut commands: Commands,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands
        .spawn(Camera2dComponents::default())
        .spawn(())
        .with(Draw {
            is_visible: false,
            ..Default::default()
        })
        .with_children(|parent| {
            parent.spawn(SpriteComponents {
                material: materials.add(Color::BLUE.into()),
                sprite: Sprite::new(Vec2::new(50.0, 50.0)),
                ..Default::default()
            });
        });
}

image

Activity

  1. added
    A-ECSEntities, components, systems, and events
    C-FeatureA new feature, making something new possible
    on Nov 11, 2020
  2. razaamir commented on Nov 11, 2020

    @razaamir
  3. cart commented on Nov 12, 2020

    @cart
    Member

    Totally agreed. Hiding a parent should hide all descendants

  4. kumorig commented on Nov 22, 2020

    @kumorig

    Similar is true for Ui components using structure:

    • NodeBundle
      • ButtonBundle
        • TextBundle
          • Text

    Setting the NodeBundle.display to Display::None hides the button, but the text is still rendered, but in the wrong position.

    Tested using current latest: 1e9a054

    Example with NodeBundle using Display::None

    use bevy::prelude::*;
    
    fn main() {
        App::build()
            .add_plugins(DefaultPlugins)
            .add_startup_system(setup)
            .run();
    }
    
    fn setup(
        commands: &mut Commands,
        asset_server: Res<AssetServer>,
        mut materials: ResMut<Assets<ColorMaterial>>,
    ) {
        commands
            // ui camera
            .spawn(UiCameraBundle::default())
            .spawn(NodeBundle {
                material: materials.add(ColorMaterial::color(Color::rgb(0.5, 0.5, 0.5).into())),
                style: Style {
                    // display: Display::Flex,
                    display: Display::None,
                    ..Default::default()
                },
                ..Default::default()
            })
            .with_children(|p| {
                p.spawn(ButtonBundle {
                    style: Style {
                        size: Size::new(Val::Px(150.0), Val::Px(65.0)),
                        margin: Rect::all(Val::Auto),
                        justify_content: JustifyContent::Center,
                        align_items: AlignItems::Center,
                        ..Default::default()
                    },
                    material: materials.add(ColorMaterial::color(Color::rgb(0.7, 0.7, 0.7).into())),
                    ..Default::default()
                })
                .with_children(|p| {
                    p.spawn(TextBundle {
                        text: Text {
                            value: "Button".to_string(),
                            font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                            style: TextStyle {
                                font_size: 40.0,
                                color: Color::rgb(0.9, 0.9, 0.9),
                                ..Default::default()
                            },
                        },
                        ..Default::default()
                    });
                });
            });
    }
    

    Display::Flex

    button_N7OzkYyGo8

    Display::None

    button_nFVsoo4u2G

    (I just assume it's the same issue) Especially since the button disappears, you would also assume the button text (or bundle?) to be hidden.

  5. rparrett commented on Feb 2, 2021

    @rparrett
    Contributor

    This workaround seems to be working for me. Posting here in case it's useful to anyone else.

    fn set_visible_recursive(
        is_visible: bool,
        entity: Entity,
        visible_query: &mut Query<&mut Visible>,
        children_query: &Query<&Children>,
    ) {
        if let Ok(mut visible) = visible_query.get_mut(entity) {
            visible.is_visible = is_visible;
        }
    
        if let Ok(children) = children_query.get(entity) {
            for child in children.iter() {
                set_visible_recursive(is_visible, *child, visible_query, children_query);
            }
        }
    }
    
    fn some_system(
      children_query: Query<&Children>,
      mut visible_query: Query<&mut Visible>,
      // ...
    ) {
        // ...
        set_visible_recursive(visible, entity, &mut visible_query, &children_query);
    }
  6. added
    C-BugAn unexpected or incorrect behavior
    and removed
    C-FeatureA new feature, making something new possible
    on Feb 17, 2021
  7. added
    A-RenderingDrawing game state to the screen
    A-UIGraphical user interfaces, styles, layouts, and widgets
    and removed
    A-ECSEntities, components, systems, and events
    on Dec 12, 2021
  8. added a commit that references this issue on Jul 15, 2022
    40d4992
  9. added a commit that references this issue on Aug 8, 2022
    938e739
  10. added a commit that references this issue on Oct 28, 2022
    8f91c12
  11. added a commit that references this issue on Feb 1, 2023
    e8bacc7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenA-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions