pub struct ChildOf(pub Entity);Expand description
Stores the parent entity of this child entity with this component.
This is a Relationship component, and creates the canonical
“parent / child” hierarchy. This is the “source of truth” component, and it pairs with
the Children RelationshipTarget.
This relationship should be used for things like:
- Organizing entities in a scene
- Propagating configuration or data inherited from a parent, such as “visibility” or “world-space global transforms”.
- Ensuring a hierarchy is despawned when an entity is despawned.
ChildOf contains a single “target” Entity. When ChildOf is inserted on a “source” entity,
the “target” entity will automatically (and immediately, via a component hook) have a Children
component inserted, and the “source” entity will be added to that Children instance.
If the ChildOf component is replaced with a different “target” entity, the old target’s Children
will be automatically (and immediately, via a component hook) be updated to reflect that change.
Likewise, when the ChildOf component is removed, the “source” entity will be removed from the old
target’s Children. If this results in Children being empty, Children will be automatically removed.
When a parent is despawned, all children (and their descendants) will also be despawned.
You can create parent-child relationships in a variety of ways. The most direct way is to insert a ChildOf component:
let root = world.spawn_empty().id();
let child1 = world.spawn(ChildOf(root)).id();
let child2 = world.spawn(ChildOf(root)).id();
let grandchild = world.spawn(ChildOf(child1)).id();
assert_eq!(&**world.entity(root).get::<Children>().unwrap(), &[child1, child2]);
assert_eq!(&**world.entity(child1).get::<Children>().unwrap(), &[grandchild]);
world.entity_mut(child2).remove::<ChildOf>();
assert_eq!(&**world.entity(root).get::<Children>().unwrap(), &[child1]);
world.entity_mut(root).despawn();
assert!(world.get_entity(root).is_err());
assert!(world.get_entity(child1).is_err());
assert!(world.get_entity(grandchild).is_err());However if you are spawning many children, you might want to use the EntityWorldMut::with_children helper instead:
let mut child1 = Entity::PLACEHOLDER;
let mut child2 = Entity::PLACEHOLDER;
let mut grandchild = Entity::PLACEHOLDER;
let root = world.spawn_empty().with_children(|p| {
child1 = p.spawn_empty().with_children(|p| {
grandchild = p.spawn_empty().id();
}).id();
child2 = p.spawn_empty().id();
}).id();
assert_eq!(&**world.entity(root).get::<Children>().unwrap(), &[child1, child2]);
assert_eq!(&**world.entity(child1).get::<Children>().unwrap(), &[grandchild]);Tuple Fields§
§0: EntityImplementations§
Trait Implementations§
Source§impl Component for ChildOf
impl Component for ChildOf
Source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
Source§type Mutability = Immutable
type Mutability = Immutable
Component<Mutability = Mutable>,
while immutable components will instead have Component<Mutability = Immutable>. Read moreSource§fn register_required_components(
_requiree: ComponentId,
required_components: &mut RequiredComponentsRegistrator<'_, '_>,
)
fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )
Source§fn on_insert() -> Option<ComponentHook>
fn on_insert() -> Option<ComponentHook>
Source§fn on_replace() -> Option<ComponentHook>
fn on_replace() -> Option<ComponentHook>
Source§fn clone_behavior() -> ComponentCloneBehavior
fn clone_behavior() -> ComponentCloneBehavior
Source§fn map_entities<M: EntityMapper>(this: &mut Self, mapper: &mut M)
fn map_entities<M: EntityMapper>(this: &mut Self, mapper: &mut M)
EntityMapper. This is used to remap entities in contexts like scenes and entity cloning.
When deriving Component, this is populated by annotating fields containing entities with #[entities] Read moreSource§fn on_add() -> Option<ComponentHook>
fn on_add() -> Option<ComponentHook>
Source§fn on_remove() -> Option<ComponentHook>
fn on_remove() -> Option<ComponentHook>
Source§fn on_despawn() -> Option<ComponentHook>
fn on_despawn() -> Option<ComponentHook>
Source§impl FromWorld for ChildOf
impl FromWorld for ChildOf
Source§fn from_world(_world: &mut World) -> Self
fn from_world(_world: &mut World) -> Self
Self using data from the given World.Source§impl Relationship for ChildOf
impl Relationship for ChildOf
Source§type RelationshipTarget = Children
type RelationshipTarget = Children
Component added to the “target” entities of this Relationship, which contains the list of all “source”
entities that relate to the “target”.Source§fn from(entity: Entity) -> Self
fn from(entity: Entity) -> Self
Relationship from the given entity.Source§fn set_risky(&mut self, entity: Entity)
fn set_risky(&mut self, entity: Entity)
Entity ID of the entity containing the RelationshipTarget to another one. Read moreSource§fn on_insert(world: DeferredWorld<'_>, _: HookContext)
fn on_insert(world: DeferredWorld<'_>, _: HookContext)
Source§fn on_replace(world: DeferredWorld<'_>, _: HookContext)
fn on_replace(world: DeferredWorld<'_>, _: HookContext)
impl Eq for ChildOf
impl StructuralPartialEq for ChildOf
Auto Trait Implementations§
impl Freeze for ChildOf
impl RefUnwindSafe for ChildOf
impl Send for ChildOf
impl Sync for ChildOf
impl Unpin for ChildOf
impl UnwindSafe for ChildOf
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut ComponentsRegistrator<'_>, ids: &mut impl FnMut(ComponentId), )
Source§fn get_component_ids(
components: &Components,
ids: &mut impl FnMut(Option<ComponentId>),
)
fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )
Source§impl<C> BundleFromComponents for Cwhere
C: Component,
impl<C> BundleFromComponents for Cwhere
C: Component,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
Source§unsafe fn get_components(
ptr: MovingPtr<'_, C>,
func: &mut impl FnMut(StorageType, OwningPtr<'_>),
) -> <C as DynamicBundle>::Effect
unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect
Source§unsafe fn apply_effect(
_ptr: MovingPtr<'_, MaybeUninit<C>>,
_entity: &mut EntityWorldMut<'_>,
)
unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.