Skip to main content

Added

Struct Added 

pub struct Added<T>(/* private fields */);
Expand description

A filter on a component that only retains results the first time after they have been added.

A common use for this filter is one-time initialization.

To retain all results without filtering but still check whether they were added 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 are visible only after deferred operations are applied, typically after the system that queued them.

§Time complexity

Added is not [ArchetypeFilter], which practically means that if the query (with T component filter) matches a million entities, Added<T> filter will iterate over all of them even if none of them were just added.

For example, these two systems are roughly equivalent in terms of performance:


fn system1(q: Query<&MyComponent, Added<Transform>>) {
    for item in &q { /* component added */ }
}

fn system2(q: Query<(&MyComponent, Ref<Transform>)>) {
    for item in &q {
        if item.1.is_added() { /* component added */ }
    }
}

§Examples


fn print_add_name_component(query: Query<&Name, Added<Name>>) {
    for name in &query {
        println!("Named entity created: {:?}", name)
    }
}

Trait Implementations§

§

impl<T> QueryFilter for Added<T>
where T: Component,

§

const IS_ARCHETYPAL: bool = false

Returns true if (and only if) this Filter relies strictly on archetypes to limit which components are accessed by the Query. Read more
§

unsafe fn filter_fetch( _state: &<Added<T> as WorldQuery>::State, fetch: &mut <Added<T> as WorldQuery>::Fetch<'_>, entity: Entity, table_row: TableRow, ) -> bool

Returns true if the provided Entity and [TableRow] should be included in the query results. If false, the entity will be skipped. Read more
§

impl<T> WorldQuery for Added<T>
where T: Component,

§

const IS_DENSE: bool

Returns true if (and only if) every table of every archetype matched by this fetch contains all of the matched components. Read more
§

type Fetch<'w> = AddedFetch<'w, T>

Per archetype/table state retrieved by this [WorldQuery] to compute Self::Item for each entity.
§

type State = ComponentId

State used to construct a 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: <Added<T> as WorldQuery>::Fetch<'wlong>, ) -> <Added<T> as WorldQuery>::Fetch<'wshort>
where 'wlong: 'wshort,

This function manually implements subtyping for the query fetches.
§

unsafe fn init_fetch<'w, 's>( world: UnsafeWorldCell<'w>, _: &'s ComponentId, last_run: Tick, this_run: Tick, ) -> <Added<T> as WorldQuery>::Fetch<'w>

Creates a new instance of 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 <Added<T> as WorldQuery>::Fetch<'w>, component_id: &'s ComponentId, _archetype: &'w Archetype, table: &'w Table, )

Adjusts internal state to account for the next [Archetype]. This will always be called on archetypes that match this [WorldQuery]. Read more
§

unsafe fn set_table<'w, 's>( fetch: &mut <Added<T> as WorldQuery>::Fetch<'w>, _: &'s ComponentId, table: &'w Table, )

Adjusts internal state to account for the next [Table]. This will always be called on tables that match this [WorldQuery]. Read more
§

fn update_component_access(_: &ComponentId, access: &mut FilteredAccess)

Adds any component accesses to the current entity used by this [WorldQuery] to access. Read more
§

fn init_state(world: &mut World) -> ComponentId

Creates and initializes a State for this [WorldQuery] type.
§

fn get_state(components: &Components) -> Option<ComponentId>

Attempts to initialize a State for this [WorldQuery] type using read-only access to [Components].
§

fn matches_component_set( _: &ComponentId, set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool

Returns true if this query matches a set of components. Otherwise, returns false. Read more
§

fn init_nested_access( _state: &Self::State, _system_name: Option<&str>, _component_access_set: &mut FilteredAccessSet, _world: UnsafeWorldCell<'_>, )

Adds any component accesses to other entities used by this [WorldQuery]. Read more
§

fn update_archetypes(_state: &mut Self::State, _world: UnsafeWorldCell<'_>)

Called when the query state is updating its archetype cache. This can be used by nested queries to update their internal archetype caches.

Auto Trait Implementations§

§

impl<T> Freeze for Added<T>

§

impl<T> RefUnwindSafe for Added<T>
where T: RefUnwindSafe,

§

impl<T> Send for Added<T>
where T: Send,

§

impl<T> Sync for Added<T>
where T: Sync,

§

impl<T> Unpin for Added<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Added<T>

§

impl<T> UnwindSafe for Added<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoResult<T> for T

§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ConditionalSend for T
where T: Send,