pub fn add_component<T: Component>(
        &mut self,
        entity: Entity,
        component: T,
    ) -> Result<(), EntityMutationError> {
        if !self.is_alive(entity) {
            return Err(EntityMutationError::DoesNotExist);
        }

        if let Some(mut comp) = self.get_component_mut(entity) {
            *comp = component;
            return Ok(());
        }

        trace!(
            world = self.id().0,
            ?entity,
            component = std::any::type_name::<T>(),
            "Adding component to entity"
        );

        // move the entity into a suitable chunk
        let target_chunk = self.move_entity(
            entity,
            &[(ComponentTypeId::of::<T>(), ComponentMeta::of::<T>())],
            &[],
            &[],
            &[],
        );

        // push new component into chunk
        let mut writer = target_chunk.writer();
        let (_, components) = writer.get();
        let slice = [component];
        unsafe {
            let components = &mut *components.get();
            components
                .get_mut(ComponentTypeId::of::<T>())
                .unwrap()
                .writer()
                .push(&slice);
        }
        std::mem::forget(slice);

        Ok(())
    }
/// Adds a component to an entity, or sets its value if the component is
/// already present.
///
/// # Notes
/// This function has the overhead of moving the entity to either an existing or new archetype,
/// causing a memory copy of the entity to a new location. This function should not be used
/// multiple times in successive order.
Adds a component to an entity, or sets its value if the component is
already present.
Adds a component to an entity or sets its value if the component is already present .
pub fn add_component<T: Component>(
        &mut self,
        entity: Entity,
        component: T,
    ) -> Result<(), EntityMutationError> {
        if !self.is_alive(entity) {
            return Err(EntityMutationError::DoesNotExist);
        }
        if let Some(mut comp) = self.get_component_mut(entity) {
            *comp = component;
            return Ok(());
        }
        trace!(
            world = self.id().0,
            ?entity,
            component = std::any::type_name::<T>(),
            "Adding component to entity"
        );
        // move the entity into a suitable chunk
        let target_chunk = self.move_entity(
            entity,
            &[(ComponentTypeId::of::<T>(), ComponentMeta::of::<T>())],
            &[],
            &[],
            &[],
        );
        // push new component into chunk
        let mut writer = target_chunk.writer();
        let (_, components) = writer.get();
        let slice = [component];
        unsafe {
            let components = &mut *components.get();
            components
                .get_mut(ComponentTypeId::of::<T>())
                .unwrap()
                .writer()
                .push(&slice);
        }
        std::mem::forget(slice);
        Ok(())
    }
pub fn add_component < T : Component > ( & mut self , entity : Entity , component : T , ) -> Result < ( ) , EntityMutationError > { if ! self . is_alive ( entity ) { return Err ( EntityMutationError :: DoesNotExist ) ; } if let Some ( mut comp ) = self . get_component_mut ( entity ) { * comp = component ; return Ok ( ( ) ) ; } trace ! ( world self id ( ) 0 entity component std any type_name T ( ) " " ) ; // move the entity into a suitable chunk let target_chunk = self . move_entity ( entity , & [ ( ComponentTypeId :: of :: < T > ( ) , ComponentMeta :: of :: < T > ( ) ) ] , & [ ] , & [ ] , & [ ] , ) ; // push new component into chunk let mut writer = target_chunk . writer ( ) ; let ( _ , components ) = writer . get ( ) ; let slice = [ component ] ; unsafe { let components = & mut * components . get ( ) ; components . get_mut ( ComponentTypeId :: of :: < T > ( ) ) . unwrap ( ) . writer ( ) . push ( & slice ) ; } std :: mem :: forget ( slice ) ; Ok ( ( ) ) }
Adds a component to an entity, or sets its value if the component is
already present.
Adds a component to an entity or sets its value if the component is already present .
&mut self
entity
Entity
component
T
&mut self
entity
Entity
component
T
notes
This function has the overhead of moving the entity to either an existing or new archetype,
causing a memory copy of the entity to a new location. This function should not be used
multiple times in successive order.
This function has the overhead of moving the entity to either an existing or new archetype causing a memory copy of the entity to a new location . This function should not be used multiple times in successive order .