Struct ChildBundles

Source
pub struct ChildBundles<B: Bundle, I: IntoIterator<Item = B>>(pub I);
Expand description

A component that, when added to an entity, will add a child entity with the given bundle.

This component will be removed from the entity immediately upon being spawned, and the supplied iterator will be iterated to completion to generate the data needed for each child. See ChildBundle for a more convenient API when adding only one child (or multiple children with distinct bundle types).

Under the hood, this is done using component lifecycle hooks.

§Examples

Just like when using Commands::spawn_batch, any iterator that returns a bundle of the same type can be used.

Working with vectors, arrays and other collections is straightforward:

use bevy_ecs::prelude::*;
use i_cant_believe_its_not_bsn::ChildBundles;

#[derive(Component)]
struct Name(&'static str);

fn spawn_hierarchy_with_vector(mut commands: Commands) {
  commands.spawn(
   (Name("Zeus"),
    ChildBundles([Name("Athena"), Name("Apollo"), Name("Hermes")])
  ));
}

However, generator-style iterators can also be used to dynamically vary the number and property of children:

use bevy_ecs::prelude::*;
use i_cant_believe_its_not_bsn::ChildBundles;

#[derive(Component)]
struct A;

#[derive(Component)]
struct ChildNumber(usize);

fn spawn_hierarchy_with_generator(mut commands: Commands) {
  commands.spawn(
   (A, // Component on parent
     ChildBundles((0..3).map(|i| (ChildNumber(i)))) // Each child will have a ChildNumber component
   ));
}

Tuple Fields§

§0: I

Trait Implementations§

Source§

impl<B: Clone + Bundle, I: Clone + IntoIterator<Item = B>> Clone for ChildBundles<B, I>

Source§

fn clone(&self) -> ChildBundles<B, I>

Returns a copy 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<B: Bundle, I: IntoIterator<Item = B> + Send + Sync + 'static> Component for ChildBundles<B, I>

Source§

const STORAGE_TYPE: StorageType = StorageType::SparseSet

This is a sparse set component as it’s only ever added and removed, never iterated over.

Source§

type Mutability = Mutable

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_component_hooks(hooks: &mut ComponentHooks)

👎Deprecated since 0.16.0: Use the individual hook methods instead (e.g., Component::on_add, etc.)
Called when registering this component, allowing mutable access to its ComponentHooks.
Source§

fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

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

fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

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

fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

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

fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

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

fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

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

fn register_required_components( _component_id: ComponentId, _components: &mut ComponentsRegistrator<'_>, _required_components: &mut RequiredComponents, _inheritance_depth: u16, _recursion_check_stack: &mut Vec<ComponentId>, )

Registers required components.
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<E>(_this: &mut Self, _mapper: &mut E)
where E: EntityMapper,

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§

impl<B: Debug + Bundle, I: Debug + IntoIterator<Item = B>> Debug for ChildBundles<B, I>

Source§

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

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

impl<B: Default + Bundle, I: Default + IntoIterator<Item = B>> Default for ChildBundles<B, I>

Source§

fn default() -> ChildBundles<B, I>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B, I> Freeze for ChildBundles<B, I>
where I: Freeze,

§

impl<B, I> RefUnwindSafe for ChildBundles<B, I>
where I: RefUnwindSafe,

§

impl<B, I> Send for ChildBundles<B, I>
where I: Send,

§

impl<B, I> Sync for ChildBundles<B, I>
where I: Sync,

§

impl<B, I> Unpin for ChildBundles<B, I>
where I: Unpin,

§

impl<B, I> UnwindSafe for ChildBundles<B, I>
where I: UnwindSafe,

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 register_required_components( components: &mut ComponentsRegistrator<'_>, required_components: &mut RequiredComponents, )

Registers components that are required by the components in this Bundle.
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<C> DynamicBundle for C
where C: Component,

Source§

type Effect = ()

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

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

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

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

Creates Self using default().

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> 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,