Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Make adding children idempotent #6763

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy_ecs::{
system::{Command, Commands, EntityCommands},
world::{EntityMut, World},
};
use bevy_log::warn;
use smallvec::SmallVec;

fn push_events(world: &mut World, events: SmallVec<[HierarchyEvent; 8]>) {
Expand Down Expand Up @@ -51,7 +52,11 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
let mut moved: SmallVec<[HierarchyEvent; 8]> = SmallVec::with_capacity(children.len());
for child in children {
if let Some(previous) = update_parent(world, *child, parent) {
debug_assert!(parent != previous);
if parent == previous {
warn!("Entity `{child:?}` is already a child of `{parent:?}`");
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

remove_from_children(world, previous, *child);
moved.push(HierarchyEvent::ChildMoved {
child: *child,
Expand Down Expand Up @@ -739,4 +744,14 @@ mod tests {
let child = world.spawn_empty().id();
world.spawn_empty().push_children(&[child]);
}

#[test]
fn push_children_idempotent() {
let mut world = World::new();
let child = world.spawn_empty().id();
world
.spawn_empty()
.push_children(&[child])
.push_children(&[child]); // this second push will warn
}
}