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>
impl<B: Clone + Bundle, I: Clone + IntoIterator<Item = B>> Clone for ChildBundles<B, I>
Source§fn clone(&self) -> ChildBundles<B, I>
fn clone(&self) -> ChildBundles<B, I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<B: Bundle, I: IntoIterator<Item = B> + Send + Sync + 'static> Component for ChildBundles<B, I>
impl<B: Bundle, I: IntoIterator<Item = B> + Send + Sync + 'static> Component for ChildBundles<B, I>
Source§const STORAGE_TYPE: StorageType = StorageType::SparseSet
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
type Mutability = Mutable
Component<Mutability = Mutable>
],
while immutable components will instead have [Component<Mutability = Immutable>
]. Read moreSource§fn register_component_hooks(hooks: &mut ComponentHooks)
fn register_component_hooks(hooks: &mut ComponentHooks)
Component::on_add
, etc.)ComponentHooks
.Source§fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn register_required_components(
_component_id: ComponentId,
_components: &mut ComponentsRegistrator<'_>,
_required_components: &mut RequiredComponents,
_inheritance_depth: u16,
_recursion_check_stack: &mut Vec<ComponentId>,
)
fn register_required_components( _component_id: ComponentId, _components: &mut ComponentsRegistrator<'_>, _required_components: &mut RequiredComponents, _inheritance_depth: u16, _recursion_check_stack: &mut Vec<ComponentId>, )
Source§fn clone_behavior() -> ComponentCloneBehavior
fn clone_behavior() -> ComponentCloneBehavior
Source§fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
fn map_entities<E>(_this: &mut Self, _mapper: &mut E)where
E: EntityMapper,
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§impl<B: Debug + Bundle, I: Debug + IntoIterator<Item = B>> Debug for ChildBundles<B, I>
impl<B: Debug + Bundle, I: Debug + IntoIterator<Item = B>> Debug for ChildBundles<B, I>
Source§impl<B: Default + Bundle, I: Default + IntoIterator<Item = B>> Default for ChildBundles<B, I>
impl<B: Default + Bundle, I: Default + IntoIterator<Item = B>> Default for ChildBundles<B, I>
Source§fn default() -> ChildBundles<B, I>
fn default() -> ChildBundles<B, I>
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> 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 register_required_components(
components: &mut ComponentsRegistrator<'_>,
required_components: &mut RequiredComponents,
)
fn register_required_components( components: &mut ComponentsRegistrator<'_>, required_components: &mut RequiredComponents, )
Bundle
.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,
fn get_components( self, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self
using default()
.