Inserting `children!` overrides existing children
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## 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.
```rust
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, 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
```
Contributor guide
Research direction
Start by tracing the `children!` macro through the insertion paths shown in the minimal repro, comparing direct insertion, chained `.insert`, `.with_child`, and `Commands` patching. Add or update coverage for entities with existing children and verify that inserting new children preserves the existing ones while the empty case behaves as intended.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100