Struct Changed
pub struct Changed<T>(/* private fields */);Expand description
A filter on a component that only retains results the first time after they have been added or mutably dereferenced.
A common use for this filter is avoiding redundant work when values have not changed.
Note that simply mutably dereferencing a component is considered a change (DerefMut).
Bevy does not compare components to their previous values.
To retain all results without filtering but still check whether they were changed after the
system last ran, use Ref<T>.
Note that this includes changes that happened before the first time this Query was run.
§Deferred
Note, that entity modifications issued with Commands
(like entity creation or entity component addition or removal) are visible only
after deferred operations are applied, typically after the system that queued them.
§Time complexity
Changed is not [ArchetypeFilter], which practically means that
if query (with T component filter) matches million entities,
Changed<T> filter will iterate over all of them even if none of them were changed.
For example, these two systems are roughly equivalent in terms of performance:
fn system1(q: Query<&MyComponent, Changed<Transform>>) {
for item in &q { /* component changed */ }
}
fn system2(q: Query<(&MyComponent, Ref<Transform>)>) {
for item in &q {
if item.1.is_changed() { /* component changed */ }
}
}§Examples
fn print_moving_objects_system(query: Query<&Name, Changed<Transform>>) {
for name in &query {
println!("Entity Moved: {:?}", name);
}
}
Trait Implementations§
§impl<T> QueryFilter for Changed<T>where
T: Component,
impl<T> QueryFilter for Changed<T>where
T: Component,
§const IS_ARCHETYPAL: bool = false
const IS_ARCHETYPAL: bool = false
§impl<T> WorldQuery for Changed<T>where
T: Component,
impl<T> WorldQuery for Changed<T>where
T: Component,
§const IS_DENSE: bool
const IS_DENSE: bool
§type Fetch<'w> = ChangedFetch<'w, T>
type Fetch<'w> = ChangedFetch<'w, T>
WorldQuery] to compute Self::Item for each entity.§type State = ComponentId
type State = ComponentId
Self::Fetch. This will be cached inside QueryState,
so it is best to move as much data / computation here as possible to reduce the cost of
constructing Self::Fetch.§fn shrink_fetch<'wlong, 'wshort>(
fetch: <Changed<T> as WorldQuery>::Fetch<'wlong>,
) -> <Changed<T> as WorldQuery>::Fetch<'wshort>where
'wlong: 'wshort,
fn shrink_fetch<'wlong, 'wshort>(
fetch: <Changed<T> as WorldQuery>::Fetch<'wlong>,
) -> <Changed<T> as WorldQuery>::Fetch<'wshort>where
'wlong: 'wshort,
§unsafe fn init_fetch<'w, 's>(
world: UnsafeWorldCell<'w>,
_: &'s ComponentId,
last_run: Tick,
this_run: Tick,
) -> <Changed<T> as WorldQuery>::Fetch<'w>
unsafe fn init_fetch<'w, 's>( world: UnsafeWorldCell<'w>, _: &'s ComponentId, last_run: Tick, this_run: Tick, ) -> <Changed<T> as WorldQuery>::Fetch<'w>
Self::Fetch,
by combining data from the World with the cached Self::State.
Readonly accesses resources registered in [WorldQuery::update_component_access]. Read more§unsafe fn set_archetype<'w, 's>(
fetch: &mut <Changed<T> as WorldQuery>::Fetch<'w>,
component_id: &'s ComponentId,
_archetype: &'w Archetype,
table: &'w Table,
)
unsafe fn set_archetype<'w, 's>( fetch: &mut <Changed<T> as WorldQuery>::Fetch<'w>, component_id: &'s ComponentId, _archetype: &'w Archetype, table: &'w Table, )
Archetype]. This will always be called on
archetypes that match this [WorldQuery]. Read more§unsafe fn set_table<'w, 's>(
fetch: &mut <Changed<T> as WorldQuery>::Fetch<'w>,
_: &'s ComponentId,
table: &'w Table,
)
unsafe fn set_table<'w, 's>( fetch: &mut <Changed<T> as WorldQuery>::Fetch<'w>, _: &'s ComponentId, table: &'w Table, )
Table]. This will always be called on tables
that match this [WorldQuery]. Read more§fn update_component_access(_: &ComponentId, access: &mut FilteredAccess)
fn update_component_access(_: &ComponentId, access: &mut FilteredAccess)
§fn init_state(world: &mut World) -> ComponentId
fn init_state(world: &mut World) -> ComponentId
State for this [WorldQuery] type.§fn matches_component_set(
_: &ComponentId,
set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool
fn matches_component_set( _: &ComponentId, set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool
§fn init_nested_access(
_state: &Self::State,
_system_name: Option<&str>,
_component_access_set: &mut FilteredAccessSet,
_world: UnsafeWorldCell<'_>,
)
fn init_nested_access( _state: &Self::State, _system_name: Option<&str>, _component_access_set: &mut FilteredAccessSet, _world: UnsafeWorldCell<'_>, )
WorldQuery]. Read more