ComponentGroup

Trait ComponentGroup 

Source
pub trait ComponentGroup: Sized {
    type UpdateError;

    // Required methods
    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§

Source

type UpdateError

The error type from the update method

Required Methods§

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§