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

Prevent setting parent as itself #8980

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Changes from 4 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
27 changes: 27 additions & 0 deletions crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,18 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

spawn_children(&mut builder);
let children = builder.push_children;
if children.children.contains(&parent) {
panic!("Entity cannot be a child of itself.");
}
self.commands().add(children);
self
}

fn push_children(&mut self, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot push entity as a child of itself.");
}
self.commands().add(PushChildren {
children: SmallVec::from(children),
parent,
Expand All @@ -361,6 +367,9 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot insert entity as a child of itself.");
}
self.commands().add(InsertChildren {
children: SmallVec::from(children),
index,
Expand All @@ -380,6 +389,9 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

fn add_child(&mut self, child: Entity) -> &mut Self {
let parent = self.id();
if child == parent {
panic!("Cannot add entity as a child of itself.");
}
self.commands().add(AddChild { child, parent });
self
}
Expand All @@ -392,6 +404,9 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot replace entity as a child of itself.");
}
self.commands().add(ReplaceChildren {
children: SmallVec::from(children),
parent,
Expand All @@ -401,6 +416,9 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

fn set_parent(&mut self, parent: Entity) -> &mut Self {
let child = self.id();
if child == parent {
panic!("Cannot set parent to itself");
}
self.commands().add(AddChild { child, parent });
self
}
Expand Down Expand Up @@ -511,6 +529,9 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {

fn add_child(&mut self, child: Entity) -> &mut Self {
let parent = self.id();
if child == parent {
panic!("Cannot add entity as a child of itself.");
}
self.world_scope(|world| {
update_old_parent(world, child, parent);
});
Expand All @@ -525,6 +546,9 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {

fn push_children(&mut self, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot push entity as a child of itself.");
}
self.world_scope(|world| {
update_old_parents(world, parent, children);
});
Expand All @@ -541,6 +565,9 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {

fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
let parent = self.id();
if children.contains(&parent) {
panic!("Cannot insert entity as a child of itself.");
}
self.world_scope(|world| {
update_old_parents(world, parent, children);
});
Expand Down