Struct valence::ecs::system::EntityCommands
pub struct EntityCommands<'a> { /* private fields */ }
Expand description
A list of commands that will be run to modify an entity.
Implementations§
§impl EntityCommands<'_>
impl EntityCommands<'_>
pub fn reborrow(&mut self) -> EntityCommands<'_>
pub fn reborrow(&mut self) -> EntityCommands<'_>
Returns an EntityCommands
with a smaller lifetime.
This is useful if you have &mut EntityCommands
but you need EntityCommands
.
pub fn insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'_>
pub fn insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'_>
Adds a Bundle
of components to the entity.
This will overwrite any previous value(s) of the same component type.
§Panics
The command will panic when applied if the associated entity does not exist.
To avoid a panic in this case, use the command Self::try_insert
instead.
§Example
#[derive(Component)]
struct Health(u32);
#[derive(Component)]
struct Strength(u32);
#[derive(Component)]
struct Defense(u32);
#[derive(Bundle)]
struct CombatBundle {
health: Health,
strength: Strength,
}
fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
commands
.entity(player.entity)
// You can insert individual components:
.insert(Defense(10))
// You can also insert pre-defined bundles of components:
.insert(CombatBundle {
health: Health(100),
strength: Strength(40),
})
// You can also insert tuples of components and bundles.
// This is equivalent to the calls above:
.insert((
Defense(10),
CombatBundle {
health: Health(100),
strength: Strength(40),
},
));
}
pub fn try_insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'_>
pub fn try_insert(&mut self, bundle: impl Bundle) -> &mut EntityCommands<'_>
Tries to add a Bundle
of components to the entity.
This will overwrite any previous value(s) of the same component type.
§Note
Unlike Self::insert
, this will not panic if the associated entity does not exist.
§Example
#[derive(Component)]
struct Health(u32);
#[derive(Component)]
struct Strength(u32);
#[derive(Component)]
struct Defense(u32);
#[derive(Bundle)]
struct CombatBundle {
health: Health,
strength: Strength,
}
fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
commands.entity(player.entity)
// You can try_insert individual components:
.try_insert(Defense(10))
// You can also insert tuples of components:
.try_insert(CombatBundle {
health: Health(100),
strength: Strength(40),
});
// Suppose this occurs in a parallel adjacent system or process
commands.entity(player.entity)
.despawn();
commands.entity(player.entity)
// This will not panic nor will it add the component
.try_insert(Defense(5));
}
pub fn remove<T>(&mut self) -> &mut EntityCommands<'_>where
T: Bundle,
pub fn remove<T>(&mut self) -> &mut EntityCommands<'_>where
T: Bundle,
Removes a Bundle
of components from the entity.
§Example
#[derive(Component)]
struct Health(u32);
#[derive(Component)]
struct Strength(u32);
#[derive(Component)]
struct Defense(u32);
#[derive(Bundle)]
struct CombatBundle {
health: Health,
strength: Strength,
}
fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
commands
.entity(player.entity)
// You can remove individual components:
.remove::<Defense>()
// You can also remove pre-defined Bundles of components:
.remove::<CombatBundle>()
// You can also remove tuples of components and bundles.
// This is equivalent to the calls above:
.remove::<(Defense, CombatBundle)>();
}
pub fn remove_by_id(
&mut self,
component_id: ComponentId,
) -> &mut EntityCommands<'_>
pub fn remove_by_id( &mut self, component_id: ComponentId, ) -> &mut EntityCommands<'_>
Removes a component from the entity.
pub fn clear(&mut self) -> &mut EntityCommands<'_>
pub fn clear(&mut self) -> &mut EntityCommands<'_>
Removes all components associated with the entity.
pub fn despawn(&mut self)
pub fn despawn(&mut self)
Despawns the entity. This will emit a warning if the entity does not exist.
See World::despawn
for more details.
§Note
This won’t clean up external references to the entity (such as parent-child relationships
if you’re using bevy_hierarchy
), which may leave the world in an invalid state.
§Example
fn remove_character_system(
mut commands: Commands,
character_to_remove: Res<CharacterToRemove>
)
{
commands.entity(character_to_remove.entity).despawn();
}
pub fn add<M>(
&mut self,
command: impl EntityCommand<M>,
) -> &mut EntityCommands<'_>where
M: 'static,
pub fn add<M>(
&mut self,
command: impl EntityCommand<M>,
) -> &mut EntityCommands<'_>where
M: 'static,
Pushes an EntityCommand
to the queue, which will get executed for the current Entity
.
§Examples
commands
.spawn_empty()
// Closures with this signature implement `EntityCommand`.
.add(|entity: EntityWorldMut| {
println!("Executed an EntityCommand for {:?}", entity.id());
});
pub fn retain<T>(&mut self) -> &mut EntityCommands<'_>where
T: Bundle,
pub fn retain<T>(&mut self) -> &mut EntityCommands<'_>where
T: Bundle,
Removes all components except the given Bundle
from the entity.
This can also be used to remove all the components from the entity by passing it an empty Bundle.
§Example
#[derive(Component)]
struct Health(u32);
#[derive(Component)]
struct Strength(u32);
#[derive(Component)]
struct Defense(u32);
#[derive(Bundle)]
struct CombatBundle {
health: Health,
strength: Strength,
}
fn remove_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
commands
.entity(player.entity)
// You can retain a pre-defined Bundle of components,
// with this removing only the Defense component
.retain::<CombatBundle>()
// You can also retain only a single component
.retain::<Health>()
// And you can remove all the components by passing in an empty Bundle
.retain::<()>();
}
pub fn log_components(&mut self)
pub fn log_components(&mut self)
Logs the components of the entity at the info level.
§Panics
The command will panic when applied if the associated entity does not exist.
pub fn observe<E, B, M>(
&mut self,
system: impl IntoObserverSystem<E, B, M>,
) -> &mut EntityCommands<'_>
pub fn observe<E, B, M>( &mut self, system: impl IntoObserverSystem<E, B, M>, ) -> &mut EntityCommands<'_>
Creates an Observer
listening for a trigger of type T
that targets this entity.
Trait Implementations§
§impl BuildChildren for EntityCommands<'_>
impl BuildChildren for EntityCommands<'_>
§fn with_children(
&mut self,
spawn_children: impl FnOnce(&mut ChildBuilder<'_>),
) -> &mut EntityCommands<'_>
fn with_children( &mut self, spawn_children: impl FnOnce(&mut ChildBuilder<'_>), ) -> &mut EntityCommands<'_>
ChildBuilder
].§fn push_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
fn push_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
§fn insert_children(
&mut self,
index: usize,
children: &[Entity],
) -> &mut EntityCommands<'_>
fn insert_children( &mut self, index: usize, children: &[Entity], ) -> &mut EntityCommands<'_>
§fn remove_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
fn remove_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
§fn add_child(&mut self, child: Entity) -> &mut EntityCommands<'_>
fn add_child(&mut self, child: Entity) -> &mut EntityCommands<'_>
§fn clear_children(&mut self) -> &mut EntityCommands<'_>
fn clear_children(&mut self) -> &mut EntityCommands<'_>
Children
] component will be removed if it exists, otherwise this does nothing.§fn replace_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
fn replace_children(&mut self, children: &[Entity]) -> &mut EntityCommands<'_>
§fn set_parent(&mut self, parent: Entity) -> &mut EntityCommands<'_>
fn set_parent(&mut self, parent: Entity) -> &mut EntityCommands<'_>
§fn remove_parent(&mut self) -> &mut EntityCommands<'_>
fn remove_parent(&mut self) -> &mut EntityCommands<'_>
Parent
] of this entity. Read more§impl DespawnRecursiveExt for EntityCommands<'_>
impl DespawnRecursiveExt for EntityCommands<'_>
§fn despawn_recursive(self)
fn despawn_recursive(self)
Despawns the provided entity and its children. This will emit warnings for any entity that does not exist.
§fn despawn_descendants(&mut self) -> &mut EntityCommands<'_>
fn despawn_descendants(&mut self) -> &mut EntityCommands<'_>
§impl ReflectCommandExt for EntityCommands<'_>
impl ReflectCommandExt for EntityCommands<'_>
§fn insert_reflect(
&mut self,
component: Box<dyn Reflect>,
) -> &mut EntityCommands<'_>
fn insert_reflect( &mut self, component: Box<dyn Reflect>, ) -> &mut EntityCommands<'_>
AppTypeRegistry
. Read more§fn insert_reflect_with_registry<T>(
&mut self,
component: Box<dyn Reflect>,
) -> &mut EntityCommands<'_>
fn insert_reflect_with_registry<T>( &mut self, component: Box<dyn Reflect>, ) -> &mut EntityCommands<'_>
insert_reflect
, but using the T
resource as type registry instead of
AppTypeRegistry
. Read more§fn remove_reflect(
&mut self,
component_type_path: impl Into<Cow<'static, str>>,
) -> &mut EntityCommands<'_>
fn remove_reflect( &mut self, component_type_path: impl Into<Cow<'static, str>>, ) -> &mut EntityCommands<'_>
AppTypeRegistry
. Read more§fn remove_reflect_with_registry<T>(
&mut self,
component_type_name: impl Into<Cow<'static, str>>,
) -> &mut EntityCommands<'_>
fn remove_reflect_with_registry<T>( &mut self, component_type_name: impl Into<Cow<'static, str>>, ) -> &mut EntityCommands<'_>
Auto Trait Implementations§
impl<'a> Freeze for EntityCommands<'a>
impl<'a> RefUnwindSafe for EntityCommands<'a>
impl<'a> Send for EntityCommands<'a>
impl<'a> Sync for EntityCommands<'a>
impl<'a> Unpin for EntityCommands<'a>
impl<'a> !UnwindSafe for EntityCommands<'a>
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
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.