如何将子代生成到现有组件中?

问题描述

假设我有这样的东西:

use bevy::prelude::*;

// Bevy style tag
struct &CharacterBox;

// Somewhere to store the entity
pub struct Action {
    pub character_Box: Option<Entity>,};

fn setup( mut commands: Commands,mut action: ResMut<Action> ) {
    if let Some(entity) = commands
        .spawn(UiCameraComponents::default())
        .spawn(NodeComponents { /* snip */ })
        .with_children(|p| {
            p.spawn(ButtonComponents { /* snip,snap */ });
        })
        .with(CharacterBox)
        .current_entity()
    {
        action.character_Box = Some(entity);
    }
}

从启动时带有一个或两个按钮的NodeComponents ...

...然后,我想从已添加的系统中添加更多按钮:

fn do_actions(
    mut commands: Commands,action: ChangedRes<Action>,mut query: Query<(&CharacterBox,&Children)>,) {

    if let Some(entity) = commands
        .spawn(ButtonComponents { /* ... */ })
        .current_entity()
    {
        let mut charBox = query.get_mut::<Children>(action.character_Box.unwrap()).unwrap();
        // I kNow this is naïve,I kNow I can't just push in the entity,// but it illustrates my point...
        charBox.push(entity); // How do I achieve this?
    }

}

如何将生成的实体(组件?)插入到NodeComponents.Children中?

如何将组件生成到现有组件中?

或者如何访问NodeComponents.Children.ChildBuilder?我可以查询ChildBuilders吗?

编辑:已删除的编辑。

解决方法

无论如何,这是我的解决方法:

 let parent_entity = action.character_box.unwrap();
 let new_entity = commands
      .spawn(ButtonComponents { /* ... */ })
      .current_entity()
      .unwrap();

 commands.push_children(parent_entity,&[c]);

(在嵌套NodeComponents的情况下,我不得不分别生成它们,然后将每个实体推入另一个实体,因为据我所知,仅通过使用{{1 }}。)