Skip to main content

ObserverBuilder

Struct ObserverBuilder 

Source
pub struct ObserverBuilder<'a, P = (), T: QueryTuple = ()> { /* private fields */ }
Expand description

ObserverBuilder is used to configure and build Observers.

Observers are systems that react to events. Observers let applications register callbacks for ECS events.

These are typically constructed via World::observer().

Implementations§

Source§

impl<'a, P: ComponentId, T: QueryTuple> ObserverBuilder<'a, P, T>

Source

pub fn new_named(world: impl WorldProvider<'a>, name: &str) -> Self

Create a new observer builder with a name

§Arguments
  • world - The world to create the observer in
  • name - The name of the observer
Source§

impl<P, T: QueryTuple> ObserverBuilder<'_, P, T>

Source

pub fn set_observer_flags(&mut self, flags: ObserverFlags) -> &mut Self

set observer flags, which are the same as Query flags

§Arguments
  • flags - the flags to set
Source

pub fn add_event( &mut self, event: impl IntoEntity, ) -> &mut ObserverBuilder<'_, (), T>

Specify the event(s) for when the observer should run.

§Arguments
  • event - The event to add
Examples found in repository?
examples/flecs/observers/observer_basics.rs (line 27)
11fn main() {
12    let world = World::new();
13
14    world
15        .observer::<flecs::OnAdd, ()>()
16        .with(Position::id())
17        .each_iter(|it, index, _| {
18            // We use .with(Position::id()) because we cannot safely access the component
19            // value here. The component value is uninitialized when the OnAdd event
20            // is emitted, which is UB in Rust. To work around this, we use .with::<T>
21            println!(" - OnAdd: {}: {}", it.event_id().to_str(), it.entity(index));
22        });
23
24    // Create an observer for three events
25    world
26        .observer::<flecs::OnSet, &Position>()
27        .add_event(flecs::OnRemove)
28        .each_iter(|it, index, pos| {
29            println!(
30                " - {}: {}: {}: with {:?}",
31                it.event().name(),
32                it.event_id().to_str(),
33                it.entity(index),
34                pos
35            );
36        });
37
38    // Create entity, set Position (emits EcsOnAdd and EcsOnSet)
39    let entity = world.entity_named("e1").set(Position { x: 10.0, y: 20.0 });
40
41    // Remove Position (emits EcsOnRemove)
42    entity.remove(Position::id());
43
44    // Remove Position again (no event emitted)
45    entity.remove(Position::id());
46
47    // Output:
48    //  - OnAdd: Position: e1
49    //  - OnSet: Position: e1: with Position { x: 10.0, y: 20.0 }
50    //  - OnRemove: Position: e1: with Position { x: 10.0, y: 20.0 }
51}
Source

pub fn yield_existing(&mut self) -> &mut Self

Invoke observer for anything that matches its query on creation

§Arguments
  • should_yield - If true, the observer will be invoked for all existing entities that match its query
Examples found in repository?
examples/flecs/observers/observer_yield_existing.rs (line 25)
15fn main() {
16    let world = World::new();
17
18    // Create existing entities with Position component
19    world.entity_named("e1").set(Position { x: 10.0, y: 20.0 });
20    world.entity_named("e2").set(Position { x: 20.0, y: 30.0 });
21
22    // Create an observer for three events
23    world
24        .observer::<flecs::OnSet, &Position>()
25        .yield_existing()
26        .each_iter(|it, index, pos| {
27            println!(
28                " - {}: {}: {}: {{ {}, {} }}",
29                it.event().name(),
30                it.event_id().to_str(),
31                it.entity(index),
32                pos.x,
33                pos.y
34            );
35        });
36
37    // Output:
38    //  - OnSet: Position: e1: { 10, 20 }
39    //  - OnSet: Position: e2: { 20, 30 }
40}

Trait Implementations§

Source§

impl<'a, P, T: QueryTuple> QueryBuilderImpl<'a> for ObserverBuilder<'a, P, T>

Source§

fn named(&mut self, name: &str) -> &mut Self

set the name of the query-like object
Source§

fn query_flags(&mut self, flags: QueryFlags) -> &mut Self

set querylags Read more
Source§

fn set_cache_kind(&mut self, kind: QueryCacheKind) -> &mut Self

Set what cache method to use for the query Read more
Source§

fn set_cached(&mut self) -> &mut Self

Set the cache method to cached
Source§

fn detect_changes(&mut self) -> &mut Self

Source§

fn expr(&mut self, expr: &'a str) -> &mut Self

set expression Read more
Source§

fn with<T>(&mut self, id: T) -> &mut Self

Source§

fn with_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>( &mut self, value: T, ) -> &mut Self

set term with enum
Source§

fn with_enum_wildcard<T: ComponentType<Enum> + ComponentId>( &mut self, ) -> &mut Self
where (Id<T>, u64): InternalIntoEntity,

set term with enum wildcard
Source§

fn without<T>(&mut self, id: T) -> &mut Self

set term without Id
Source§

fn without_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>( &mut self, value: T, ) -> &mut Self

set term without enum
Source§

fn without_enum_wildcard<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>( &mut self, ) -> &mut Self
where (Id<T>, u64): InternalIntoEntity,

set term without enum wildcard
Source§

fn term(&mut self) -> &mut Self

Term notation for more complex query features Read more
Source§

fn term_at_type<T: ComponentId>(&mut self) -> &mut Self

Sets the current term to the one with the provided type. This loops over all terms to find the one with the provided type. For performance-critical paths, use term_at(index: u32) instead.
Source§

fn term_at(&mut self, index: u32) -> &mut Self

Sets the current term to the one at the provided index.
Source§

fn term_at_checked<T: ComponentId>(&mut self, index: u32) -> &mut Self

Set the current term to the one with the provided id and assert that the type matches. this does not do the type checking in release unless flecs_force_build_debug_c or flecs_force_enable_ecs_asserts is enabled.
Source§

fn write<T>(&mut self, id: T) -> &mut Self

Set the id as current term and in mode out
Source§

fn write_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>( &mut self, value: T, ) -> &mut Self

Set the type as current term and in mode out
Source§

fn read<T>(&mut self, id: T) -> &mut Self

Set the id as current term and in mode in
Source§

fn read_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>( &mut self, value: T, ) -> &mut Self

Set the type as current term and in mode in
Source§

fn scope_open(&mut self) -> &mut Self

Open a scope for the query
Source§

fn scope_close(&mut self) -> &mut Self

Close a scope for the query
Source§

fn order_by<T>(&mut self, compare: impl OrderByFn<T>) -> &mut Self
where T: ComponentId, Self: QueryBuilderImpl<'a>,

Sorts the output of a query. Read more
Source§

fn order_by_id( &mut self, component: impl Into<Entity>, compare: impl OrderByFnVoid, ) -> &mut Self

Sorts the output of a query. Read more
Source§

fn group_by_fn( &mut self, component: impl IntoEntity, group_by_action: ecs_group_by_action_t, ) -> &mut Self

Group and sort matched tables. Read more
Source§

fn group_by(&mut self, component: impl IntoEntity) -> &mut Self

Group and sort matched tables. Read more
Source§

fn group_by_ctx( &mut self, ctx: *mut c_void, ctx_free: ecs_ctx_free_t, ) -> &mut Self

Specify context to be passed to the group_by function. Read more
Source§

fn on_group_create(&mut self, action: ecs_group_create_action_t) -> &mut Self

Specify the on_group_create action. Read more
Source§

fn on_group_delete(&mut self, action: ecs_group_delete_action_t) -> &mut Self

Specify the on_group_delete action. Read more
Source§

impl<'a, P, T> SystemAPI<'a, P, T> for ObserverBuilder<'a, P, T>
where T: QueryTuple, P: ComponentId,

Source§

fn set_context(&mut self, context: *mut c_void) -> &mut Self

Set context
Source§

fn each<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(T::TupleType<'_>) + 'static,

Each iterator for systems. Read more
Source§

unsafe fn each_unchecked<Func>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(T::TupleType<'_>) + 'static,

Each iterator for systems without aliasing checks. Read more
Source§

fn each_internal<Func, const CHECKED: bool>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(T::TupleType<'_>) + 'static,

Internal implementation for both checked and unchecked each iteration.
Source§

fn each_entity<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,

Each entity iterator for systems. Read more
Source§

unsafe fn each_entity_unchecked<Func>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,

Each entity iterator for systems without aliasing checks. Read more
Source§

fn each_entity_internal<Func, const CHECKED: bool>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,

Internal implementation for both checked and unchecked each_entity iteration.
Source§

fn each_iter<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, false, P>, FieldIndex, T::TupleType<'_>) + 'static,

Each iterator. This variant of each provides access to the TableIter object, which contains more information about the object being iterated. The usize argument contains the index of the entity being iterated, which can be used to obtain entity-specific data from the TableIter object. Read more
Source§

unsafe fn each_iter_unchecked<Func>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, false, P>, FieldIndex, T::TupleType<'_>) + 'static,

Unchecked version of each_iter that bypasses mutable aliasing checks. Read more
Source§

fn each_iter_internal<Func, const CHECKED: bool>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, false, P>, FieldIndex, T::TupleType<'_>) + 'static,

Internal implementation for each_iter and each_iter_unchecked. Read more
Source§

fn run<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, true, P>) + 'static,

Run iterator. This operation expects manual iteration over the tables with iter.next() and iter.iter(). Read more
Source§

fn run_each<Func, FuncEach>( &mut self, func: Func, func_each: FuncEach, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, true, P>) + 'static, FuncEach: FnMut(T::TupleType<'_>) + 'static,

Run iterator with each forwarding. This operation expects manual iteration over the tables with iter.next() and iter.each() Read more
Source§

fn run_each_entity<Func, FuncEachEntity>( &mut self, func: Func, func_each_entity: FuncEachEntity, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, true, P>) + 'static, FuncEachEntity: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,

Run iterator with each entity forwarding. This operation expects manual iteration over the tables with iter.next() and iter.each() Read more
Source§

fn run_each_iter<Func, FuncEachIter>( &mut self, func: Func, func_each_iter: FuncEachIter, ) -> <Self as Builder<'a>>::BuiltType
where Func: FnMut(TableIter<'_, true, P>) + 'static, FuncEachIter: FnMut(TableIter<'_, true, P>, FieldIndex, T::TupleType<'_>) + 'static,

Source§

impl<'a, P, T: QueryTuple> TermBuilderImpl<'a> for ObserverBuilder<'a, P, T>

Source§

fn init_current_term<T>(&mut self, id: T)
where T: IntoId,

initializes a new term from a id of a component or pair Read more
Source§

fn init_term_from<T: ComponentOrPairId>(&mut self)

initialize a new term from a component or pair Read more
Source§

fn reset(&mut self)

Reset the term
Source§

fn is_set(&mut self) -> bool

Check if term is initialized Read more
Source§

fn id(&self) -> Id

Get the term id of the current term set Read more
Source§

fn inout(&self) -> InOutKind

Get the inout type of term of the current term set
Source§

fn oper(&self) -> OperKind

Get the operator of term of the current term set
Source§

fn src_id(&self) -> Entity

Get the src id of term of the current term set
Source§

fn first_id(&self) -> Entity

Get the first of term of the current term set
Source§

fn second_id(&self) -> Entity

Get the second of term of the current term set
Source§

fn self_(&mut self) -> &mut Self

The self flag indicates the term identifier itself is used
Source§

fn set_id(&mut self, id: impl IntoEntity) -> &mut Self

Specify value of identifier by id, same as id() of the current term set Read more
Source§

fn entity(&mut self, entity: impl Into<Entity>) -> &mut Self

Specify value of identifier by id. Almost the same as id(entity), but this operation explicitly sets the flecs::IsEntity flag. This forces the id to be interpreted as entity, whereas not setting the flag would implicitly convert ids for builtin variables such as flecs::This to a variable. Read more
Source§

fn name(&mut self, name: &'a str) -> &mut Self

Specify value of identifier by name Read more
Source§

fn set_var(&mut self, var_name: &'a str) -> &mut Self

Specify identifier is a variable (resolved at query evaluation time) Read more
Source§

fn flags(&mut self, flags: u64) -> &mut Self

Override term id flags Read more
Source§

fn src(&mut self) -> &mut Self

Call prior to setting values for src identifier
Source§

fn first(&mut self) -> &mut Self

Call prior to setting values for first identifier. This is either the component identifier, or first element of a pair (in case second is populated as well).
Source§

fn second(&mut self) -> &mut Self

Call prior to setting values for second identifier. This is the second element of a pair. Requires that first() is populated as well.
Source§

fn set_src<T: SingleAccessArg>(&mut self, id: T) -> &mut Self

Select src identifier Read more
Source§

fn set_first<Q: SingleAccessArg>(&mut self, id: Q) -> &mut Self

Select first identifier Read more
Source§

fn set_second<T: SingleAccessArg>(&mut self, id: T) -> &mut Self

Select second identifier Read more
Source§

fn up(&mut self) -> &mut Self

default up where trav is set to 0. The up flag indicates that the term identifier may be substituted by traversing a relationship upwards. For example: substitute the identifier with its parent by traversing the ChildOf relationship.
Source§

fn parent(&mut self) -> &mut Self

same as up
Source§

fn up_id(&mut self, traverse_relationship: impl IntoEntity) -> &mut Self

The up flag indicates that the term identifier may be substituted by traversing a relationship upwards. For example: substitute the identifier with its parent by traversing the ChildOf relationship. Read more
Source§

fn cascade(&mut self) -> &mut Self

Cascade iterates a hierarchy in top to bottom order (breadth first search) The cascade flag is like up, but returns results in breadth-first order. Only supported for flecs::query.
Source§

fn cascade_id(&mut self, traverse_relationship: impl IntoEntity) -> &mut Self

Cascade iterates a hierarchy in top to bottom order (breadth first search) The cascade flag is like up, but returns results in breadth-first order. Only supported for flecs::query Read more
Source§

fn desc(&mut self) -> &mut Self

Use with cascade to iterate results in descending (bottom + top) order.
Source§

fn trav( &mut self, traverse_relationship: impl IntoEntity, flags: u64, ) -> &mut Self

Specify relationship to traverse, and flags to indicate direction Read more
Source§

fn id_flags(&mut self, flags: impl IntoId) -> &mut Self

Set id flags for term. Read more
Source§

fn set_inout_kind(&mut self, inout: InOutKind) -> &mut Self

Set read/write access of term Read more
Source§

fn inout_stage(&mut self, inout: InOutKind) -> &mut Self

Set read/write access for stage. Use this when a system reads or writes components other than the ones provided by the query. This information can be used by schedulers to insert sync/merge points between systems where deferred operations are flushed. Read more
Source§

fn write_curr(&mut self) -> &mut Self

Set write mode on current term. Short for inout_stage(InOutKind::Out). Use when system uses add, remove or set. Read more
Source§

fn read_curr(&mut self) -> &mut Self

Set read mode on current term. Short for inout_stage(InOutKind::In). Use when system uses get Read more
Source§

fn read_write_curr(&mut self) -> &mut Self

Short for inout_stage(InOutKind::InOut). Use when system uses ensure Read more
Source§

fn set_in(&mut self) -> &mut Self

short for set_inout(InOutKind::In) Read more
Source§

fn set_out(&mut self) -> &mut Self

short for set_inout(InOutKind::Out) Read more
Source§

fn set_inout(&mut self) -> &mut Self

short for set_inout(InOutKind::InOut) Read more
Source§

fn set_inout_none(&mut self) -> &mut Self

short for set_inout(InOutKind::None) Read more
Source§

fn set_oper(&mut self, oper: OperKind) -> &mut Self

set operator of term Read more
Source§

fn and(&mut self) -> &mut Self

short for set_oper(OperKind::And) Read more
Source§

fn or(&mut self) -> &mut Self

short for set_oper(OperKind::Or) Read more
Source§

fn not(&mut self) -> &mut Self

short for set_oper(OperKind::Not) Read more
Source§

fn optional(&mut self) -> &mut Self

short for set_oper(OperKind::Optional) Read more
Source§

fn and_from(&mut self) -> &mut Self

short for set_oper(OperKind::AndFrom) Read more
Source§

fn or_from(&mut self) -> &mut Self

short for set_oper(OperKind::OrFrom) Read more
Source§

fn not_from(&mut self) -> &mut Self

short for set_oper(OperKind::NotFrom) Read more
Source§

fn filter(&mut self) -> &mut Self

Query terms are not triggered on by observers
Source§

impl<'a, P, T: QueryTuple> WorldProvider<'a> for ObserverBuilder<'a, P, T>

Source§

fn world(&self) -> WorldRef<'a>

Auto Trait Implementations§

§

impl<'a, P = (), T = ()> !Send for ObserverBuilder<'a, P, T>

§

impl<'a, P = (), T = ()> !Sync for ObserverBuilder<'a, P, T>

§

impl<'a, P, T> Freeze for ObserverBuilder<'a, P, T>

§

impl<'a, P, T> RefUnwindSafe for ObserverBuilder<'a, P, T>

§

impl<'a, P, T> Unpin for ObserverBuilder<'a, P, T>
where PhantomData<&'a (T, P)>: Unpin,

§

impl<'a, P, T> UnsafeUnpin for ObserverBuilder<'a, P, T>

§

impl<'a, P, T> UnwindSafe for ObserverBuilder<'a, P, T>

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> DoesNotImpl for T

Source§

const IMPLS: bool = false

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.

Source§

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

Source§

type Error = !

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.