Trait component_group::ComponentGroup
source · pub trait ComponentGroup: Sized {
type UpdateError;
fn first_from_world(world: &World) -> Option<(Entity, Self)>;
fn from_world(world: &World, entity: Entity) -> Self;
fn create(self, world: &mut World) -> Entity;
fn update(
self,
world: &mut World,
entity: Entity
) -> Result<(), Self::UpdateError>;
fn remove(world: &mut World, entity: Entity) -> Self;
}
Expand description
Represents a group of specs::Component
fields that can be added or extracted from
a specs::World
.
To automatically derive this trait using #[derive(ComponentGroup)]
, all components within
the group must implement the Clone
trait.
See the top-level crate documentation for more details.
Required Associated Types§
sourcetype UpdateError
type UpdateError
The error type from the update
method
Required Methods§
sourcefn first_from_world(world: &World) -> Option<(Entity, Self)>
fn first_from_world(world: &World) -> Option<(Entity, Self)>
Extracts the first instance of this component group from the world.
This method is convenient if you know that there is exactly one instance of a this group in the world.
Returns None
if any of the required fields could not be populated. Fields with an
Option
type will be set to None
if their component could not be populated.
sourcefn from_world(world: &World, entity: Entity) -> Self
fn from_world(world: &World, entity: Entity) -> Self
Extracts this group of components for the given entity from the given world.
Panics if one of the component fields could not be populated. This can happen if the
component does not exist for this entity. If the field is an Option
type, its value will
be set to None
instead of panicking.
sourcefn create(self, world: &mut World) -> Entity
fn create(self, world: &mut World) -> Entity
Creates a new entity in the world and adds all the components from this group to that entity.
Any fields with a value of None
will not be added to the created entity.
sourcefn update(
self,
world: &mut World,
entity: Entity
) -> Result<(), Self::UpdateError>
fn update(
self,
world: &mut World,
entity: Entity
) -> Result<(), Self::UpdateError>
Update the components of a given entity with all of the components from this group.
Any fields with a value of None
will be explicitly removed from the given entity.
Note: Any additional components that the entity has other than the ones covered by the fields of this group will be left untouched.
sourcefn remove(world: &mut World, entity: Entity) -> Self
fn remove(world: &mut World, entity: Entity) -> Self
Removes all the components from this group from their storages in the given world for the given entity. Returns the values of the removed components.
Note: Any additional components that the entity has other than the ones covered by the fields of this group will be left untouched.
Panics if one of the required component fields was not present for removal. If the field is
an Option
type, its value when returned will be set to None
instead of panicking.