Trait bevy_ecs::bundle::Bundle [−][src]
pub unsafe trait Bundle: Send + Sync + 'static {
fn component_ids(
components: &mut Components,
storages: &mut Storages
) -> Vec<ComponentId>;
unsafe fn from_components(func: impl FnMut() -> *mut u8) -> Self
where
Self: Sized;
fn get_components(self, func: impl FnMut(*mut u8));
}
Expand description
An ordered collection of Component
s.
Commonly used for spawning entities and adding and removing components in bulk. This
trait is automatically implemented for tuples of components: (ComponentA, ComponentB)
is a very convenient shorthand when working with one-off collections of components. Note
that both the unit type ()
and (ComponentA, )
are valid bundles. The unit bundle is
particularly useful for spawning multiple empty entities by using
Commands::spawn_batch
.
Examples
Typically, you will simply use #[derive(Bundle)]
when creating your own Bundle
. Each
struct field is a component:
#[derive(Bundle)]
struct MyBundle {
a: ComponentA,
b: ComponentB,
c: ComponentC,
}
You can nest bundles using the #[bundle]
attribute:
#[derive(Component)]
struct X(i32);
#[derive(Component)]
struct Y(u64);
#[derive(Component)]
struct Z(String);
#[derive(Bundle)]
struct A {
x: X,
y: Y,
}
#[derive(Bundle)]
struct B {
#[bundle]
a: A,
z: Z,
}
Safety
Bundle::component_ids
must return theComponentId
for each component type in the bundle, in the exact order thatBundle::get_components
is called.Bundle::from_components
must callfunc
exactly once for eachComponentId
returned byBundle::component_ids
.
Required methods
fn component_ids(
components: &mut Components,
storages: &mut Storages
) -> Vec<ComponentId>
fn component_ids(
components: &mut Components,
storages: &mut Storages
) -> Vec<ComponentId>
fn get_components(self, func: impl FnMut(*mut u8))
fn get_components(self, func: impl FnMut(*mut u8))
Calls func
on each value, in the order of this bundle’s Component
s. This will
std::mem::forget
the bundle fields, so callers are responsible for dropping the fields
if that is desirable.