pub struct Merge<T>(pub T)
where
T: MergeComponent;Expand description
An EntityCommand which is used to add components.
§Usage
It is impossible to have duplicate components on an Entity in Bevy.
However, in some cases, multiple instances of some components can be “merged” into one.
If a component implements MergeComponent, you can use this command to merge multiple instances
of the component into one.
use bevy::prelude::*;
use moonshine_util::prelude::*;
#[derive(Component, Default)]
struct N(usize);
impl MergeComponent for N {
fn merge(&mut self, other: Self) {
self.0 += other.0;
}
}
let mut world = World::new();
let entity = world.spawn_empty().id();
world.commands().entity(entity).queue(Merge(N(1)));
world.commands().entity(entity).queue(Merge(N(2)));
world.flush();
let &N(value) = world.get(entity).unwrap();
assert_eq!(value, 3);This command may also be used as a Component itself. This can be used in a Bundle or as a
requirement to merge components.
use bevy::prelude::*;
use moonshine_util::prelude::*;
#[derive(Component, Default)]
struct N(usize);
impl MergeComponent for N {
fn merge(&mut self, other: Self) {
self.0 += other.0;
}
}
let mut world = World::new();
let entity = world.spawn((N(1), Merge(N(2))));
let &N(value) = entity.get().unwrap();
assert_eq!(value, 3);Because Merge<T> is a component itself, it can be used as a component requirement.
However, because of the component uniqueness rule, multiple Merge<T> instances may not exist on the same entity.
To work around this, you can use MergeFrom and MergeWith.
Tuple Fields§
§0: TImplementations§
Trait Implementations§
Source§impl<T> Component for Merge<T>
impl<T> Component for Merge<T>
Source§const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::Table
A constant indicating the storage type used for this component.
Source§type Mutability = Mutable
type Mutability = Mutable
A marker type to assist Bevy with determining if this component is
mutable, or immutable. Mutable components will have
Component<Mutability = Mutable>,
while immutable components will instead have Component<Mutability = Immutable>. Read moreSource§fn register_required_components(
_requiree: ComponentId,
required_components: &mut RequiredComponentsRegistrator<'_, '_>,
)
fn register_required_components( _requiree: ComponentId, required_components: &mut RequiredComponentsRegistrator<'_, '_>, )
Registers required components. Read more
Source§fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
Source§fn clone_behavior() -> ComponentCloneBehavior
fn clone_behavior() -> ComponentCloneBehavior
Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component. Read more
Source§fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Merge<T>>>
fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Merge<T>>>
Returns
ComponentRelationshipAccessor required for working with relationships in dynamic contexts. Read moreSource§fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>
fn on_add() -> 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 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,
Maps the entities on this component using the given
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<T> EntityCommand for Merge<T>where
T: MergeComponent,
impl<T> EntityCommand for Merge<T>where
T: MergeComponent,
Source§fn apply(self, entity: EntityWorldMut<'_>)
fn apply(self, entity: EntityWorldMut<'_>)
Executes this command for the given
Entity.Auto Trait Implementations§
impl<T> Freeze for Merge<T>where
T: Freeze,
impl<T> RefUnwindSafe for Merge<T>where
T: RefUnwindSafe,
impl<T> Send for Merge<T>
impl<T> Sync for Merge<T>
impl<T> Unpin for Merge<T>where
T: Unpin,
impl<T> UnwindSafe for Merge<T>where
T: 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
Mutably borrows from an owned value. Read more
Source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut ComponentsRegistrator<'_>, ) -> impl Iterator<Item = ComponentId> + use<C>
Source§fn get_component_ids(
components: &Components,
) -> impl Iterator<Item = Option<ComponentId>>
fn get_component_ids( components: &Components, ) -> impl Iterator<Item = Option<ComponentId>>
Source§impl<C> BundleFromComponents for Cwhere
C: Component,
impl<C> BundleFromComponents for Cwhere
C: Component,
Source§impl<C> CommandWithEntity<Result<(), EntityMutableFetchError>> for Cwhere
C: EntityCommand,
impl<C> CommandWithEntity<Result<(), EntityMutableFetchError>> for Cwhere
C: EntityCommand,
Source§fn with_entity(
self,
entity: Entity,
) -> impl Command<Result<(), EntityMutableFetchError>> + HandleError<Result<(), EntityMutableFetchError>>
fn with_entity( self, entity: Entity, ) -> impl Command<Result<(), EntityMutableFetchError>> + HandleError<Result<(), EntityMutableFetchError>>
Passes in a specific entity to an
EntityCommand, resulting in a Command that
internally runs the EntityCommand on that entity.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
Source§unsafe fn get_components(
ptr: MovingPtr<'_, C>,
func: &mut impl FnMut(StorageType, OwningPtr<'_>),
) -> <C as DynamicBundle>::Effect
unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect
Moves the components out of the bundle. Read more
Source§unsafe fn apply_effect(
_ptr: MovingPtr<'_, MaybeUninit<C>>,
_entity: &mut EntityWorldMut<'_>,
)
unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )
Applies the after-effects of spawning this bundle. Read more
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Converts this type into the system output type.
Source§impl<T> Kind for Twhere
T: Component,
impl<T> Kind for Twhere
T: Component,
Source§type Filter = With<T>
type Filter = With<T>
The
QueryFilter which defines this kind.Source§fn debug_name() -> String
fn debug_name() -> String
Returns the debug name of this kind. Read more