Skip to content

Inserting children! overrides existing children #19715

Open
@Pascualex

Description

@Pascualex

Bevy version

v0.16.1

What you did

Inserted the children! macro on an entity that had existing children.

What went wrong

Instead of appending the new children, the existing ones got overridden.

Minimal repro

I wanted to cover a variety of scenarios but the override seems to be consistent.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(
            Startup,
            (
                spawn_entities,
                print_children,
                patch_entity_f,
                print_children,
            )
                .chain(),
        )
        .run();
}

#[derive(Resource)]
struct EntityF(Entity);

fn spawn_entities(mut commands: Commands) {
    commands.spawn((Name::new("Entity A"), children![(), ()]));
    commands
        .spawn((Name::new("Entity B"), children![()]))
        .insert(children![()]);
    commands
        .spawn(Name::new("Entity C"))
        .insert(children![()])
        .insert(children![()]);
    commands
        .spawn(Name::new("Entity D"))
        .with_child(())
        .with_child(());
    commands
        .spawn(Name::new("Entity E"))
        .with_child(())
        .with_child(())
        .insert(children![]);
    let entity_f = commands
        .spawn(Name::new("Entity F"))
        .with_child(())
        .with_child(())
        .id();
    commands.insert_resource(EntityF(entity_f));
}

fn patch_entity_f(entity_f: Res<EntityF>, mut commands: Commands) {
    commands.entity(entity_f.0).insert(children![]);
}

fn print_children(entities: Query<(&Name, Option<&Children>)>) {
    for (name, children) in &entities {
        let children_count = children.map(Children::len).unwrap_or(0);
        println!("{} has {} children", name, children_count);
    }
}

The output:

Entity A has 2 children
Entity B has 1 children
Entity C has 1 children
Entity D has 2 children
Entity E has 0 children
Entity F has 2 children

Entity A has 2 children
Entity B has 1 children
Entity C has 1 children
Entity D has 2 children
Entity E has 0 children
Entity F has 0 children

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorD-ModestA "normal" level of difficulty; suitable for simple features or challenging fixesS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions