Skip to content

Commit

Permalink
Parent -> ChildOf (#17427)
Browse files Browse the repository at this point in the history
Fixes #17412

## Objective

`Parent` uses the "has a X" naming convention. There is increasing
sentiment that we should use the "is a X" naming convention for
relationships (following #17398). This leaves `Children` as-is because
there is prevailing sentiment that `Children` is clearer than `ParentOf`
in many cases (especially when treating it like a collection).

This renames `Parent` to `ChildOf`.

This is just the implementation PR. To discuss the path forward, do so
in #17412.

## Migration Guide

- The `Parent` component has been renamed to `ChildOf`.
  • Loading branch information
cart authored Jan 20, 2025
1 parent 000c362 commit ba5e71f
Show file tree
Hide file tree
Showing 34 changed files with 203 additions and 197 deletions.
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/entity_cloning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::hint::black_box;
use benches::bench;
use bevy_ecs::bundle::Bundle;
use bevy_ecs::component::ComponentCloneHandler;
use bevy_ecs::hierarchy::Parent;
use bevy_ecs::hierarchy::ChildOf;
use bevy_ecs::reflect::AppTypeRegistry;
use bevy_ecs::{component::Component, world::World};
use bevy_math::Mat4;
Expand Down Expand Up @@ -142,7 +142,7 @@ fn bench_clone_hierarchy<B: Bundle + Default + GetTypeRegistration>(

for parent_id in current_hierarchy_level {
for _ in 0..children {
let child_id = world.spawn((B::default(), Parent(parent_id))).id();
let child_id = world.spawn((B::default(), ChildOf(parent_id))).id();
hierarchy_level.push(child_id);
}
}
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/observers/propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn event_propagation(criterion: &mut Criterion) {
struct TestEvent<const N: usize> {}

impl<const N: usize> Event for TestEvent<N> {
type Traversal = &'static Parent;
type Traversal = &'static ChildOf;
const AUTO_PROPAGATE: bool = true;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Default for App {
{
app.init_resource::<AppTypeRegistry>();
app.register_type::<Name>();
app.register_type::<Parent>();
app.register_type::<ChildOf>();
app.register_type::<Children>();
}

Expand Down
19 changes: 11 additions & 8 deletions crates/bevy_ecs/src/entity/clone_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
bundle::Bundle,
component::{Component, ComponentCloneHandler, ComponentId, ComponentInfo, Components},
entity::Entity,
hierarchy::{Children, Parent},
hierarchy::{ChildOf, Children},
query::DebugCheckedUnwrap,
world::{DeferredWorld, World},
};
Expand Down Expand Up @@ -631,11 +631,11 @@ impl<'w> EntityCloneBuilder<'w> {
/// Sets the option to add cloned entity as a child to the parent entity.
pub fn as_child(&mut self, as_child: bool) -> &mut Self {
if as_child {
self.override_component_clone_handler::<Parent>(ComponentCloneHandler::custom_handler(
self.override_component_clone_handler::<ChildOf>(ComponentCloneHandler::custom_handler(
component_clone_parent,
))
} else {
self.remove_component_clone_handler_override::<Parent>()
self.remove_component_clone_handler_override::<ChildOf>()
}
}

Expand Down Expand Up @@ -694,18 +694,21 @@ fn component_clone_children(world: &mut DeferredWorld, ctx: &mut ComponentCloneC
.with_source_and_target(*child, child_clone);
world.commands().queue(move |world: &mut World| {
clone_entity.clone_entity(world);
world.entity_mut(child_clone).insert(Parent(parent));
world.entity_mut(child_clone).insert(ChildOf(parent));
});
}
}

/// Clone handler for the [`Parent`] component. Allows to add clone as a child to the parent entity.
/// Clone handler for the [`ChildOf`] component. Allows to add clone as a child to the parent entity.
fn component_clone_parent(world: &mut DeferredWorld, ctx: &mut ComponentCloneCtx) {
let parent = ctx
.read_source_component::<Parent>()
.read_source_component::<ChildOf>()
.map(|p| p.0)
.expect("Source entity must have Parent component");
world.commands().entity(ctx.target()).insert(Parent(parent));
.expect("Source entity must have a ChildOf component");
world
.commands()
.entity(ctx.target())
.insert(ChildOf(parent));
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit ba5e71f

Please sign in to comment.