ChildOf

Struct ChildOf 

Source
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:

  1. Organizing entities in a scene
  2. Propagating configuration or data inherited from a parent, such as “visibility” or “world-space global transforms”.
  3. 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: Entity

Implementations§

Source§

impl ChildOf

Source

pub fn parent(&self) -> Entity

The parent entity of this child entity.

Trait Implementations§

Source§

impl Clone for ChildOf

Source§

fn clone(&self) -> ChildOf

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Component for ChildOf
where Self: Send + Sync + 'static,

Source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table

A constant indicating the storage type used for this component.
Source§

type Mutability = Immutable

A marker type to assist Bevy with determining if this component is mutable, or immutable. Mutable components will have Component<Mutability = Mutable>, while immutable components will instead have Component<Mutability = Immutable>. Read more
Source§

fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )

Registers required components. Read more
Source§

fn on_insert() -> Option<ComponentHook>

Gets the on_insert ComponentHook for this Component if one is defined.
Source§

fn on_replace() -> Option<ComponentHook>

Gets the on_replace ComponentHook for this Component if one is defined.
Source§

fn clone_behavior() -> ComponentCloneBehavior

Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component. Read more
Source§

fn map_entities<M: EntityMapper>(this: &mut Self, mapper: &mut M)

Maps the entities on this component using the given 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 more
Source§

fn on_add() -> Option<ComponentHook>

Gets the on_add ComponentHook for this Component if one is defined.
Source§

fn on_remove() -> Option<ComponentHook>

Gets the on_remove ComponentHook for this Component if one is defined.
Source§

fn on_despawn() -> Option<ComponentHook>

Gets the on_despawn ComponentHook for this Component if one is defined.
Source§

impl Debug for ChildOf

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromWorld for ChildOf

Source§

fn from_world(_world: &mut World) -> Self

Creates Self using data from the given World.
Source§

impl PartialEq for ChildOf

Source§

fn eq(&self, other: &ChildOf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Relationship for ChildOf

Source§

type RelationshipTarget = Children

The Component added to the “target” entities of this Relationship, which contains the list of all “source” entities that relate to the “target”.
Source§

fn get(&self) -> Entity

Gets the Entity ID of the related entity.
Source§

fn from(entity: Entity) -> Self

Creates this Relationship from the given entity.
Source§

fn set_risky(&mut self, entity: Entity)

Changes the current Entity ID of the entity containing the RelationshipTarget to another one. Read more
Source§

fn on_insert(world: DeferredWorld<'_>, _: HookContext)

The on_insert component hook that maintains the Relationship / RelationshipTarget connection.
Source§

fn on_replace(world: DeferredWorld<'_>, _: HookContext)

The on_replace component hook that maintains the Relationship / RelationshipTarget connection.
Source§

impl Eq for ChildOf

Source§

impl StructuralPartialEq for ChildOf

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> Bundle for C
where C: Component,

Source§

fn component_ids( components: &mut ComponentsRegistrator<'_>, ids: &mut impl FnMut(ComponentId), )

Source§

fn get_component_ids( components: &Components, ids: &mut impl FnMut(Option<ComponentId>), )

Gets this Bundle’s component ids. This will be None if the component has not been registered.
Source§

impl<C> BundleFromComponents for C
where C: Component,

Source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynEq for T
where T: Any + Eq,

Source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

This method tests for self and other values to be equal. Read more
Source§

impl<C> DynamicBundle for C
where C: Component,

Source§

type Effect = ()

An operation on the entity that happens after inserting this bundle.
Source§

unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect

Moves the components out of the bundle. Read more
Source§

unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )

Applies the after-effects of spawning this bundle. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ConditionalSend for T
where T: Send,