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>
impl<'a, P: ComponentId, T: QueryTuple> ObserverBuilder<'a, P, T>
Sourcepub fn new_named(world: impl WorldProvider<'a>, name: &str) -> Self
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 inname- The name of the observer
Source§impl<P, T: QueryTuple> ObserverBuilder<'_, P, T>
impl<P, T: QueryTuple> ObserverBuilder<'_, P, T>
Sourcepub fn set_observer_flags(&mut self, flags: ObserverFlags) -> &mut Self
pub fn set_observer_flags(&mut self, flags: ObserverFlags) -> &mut Self
Sourcepub fn add_event(
&mut self,
event: impl IntoEntity,
) -> &mut ObserverBuilder<'_, (), T>
pub fn add_event( &mut self, event: impl IntoEntity, ) -> &mut ObserverBuilder<'_, (), T>
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}Sourcepub fn yield_existing(&mut self) -> &mut Self
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>
impl<'a, P, T: QueryTuple> QueryBuilderImpl<'a> for ObserverBuilder<'a, P, T>
Source§fn query_flags(&mut self, flags: QueryFlags) -> &mut Self
fn query_flags(&mut self, flags: QueryFlags) -> &mut Self
set querylags Read more
Source§fn set_cache_kind(&mut self, kind: QueryCacheKind) -> &mut Self
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
fn set_cached(&mut self) -> &mut Self
Set the cache method to cached
fn detect_changes(&mut self) -> &mut Self
fn with<T>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
Source§fn with_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>(
&mut self,
value: T,
) -> &mut Self
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 Selfwhere
(Id<T>, u64): InternalIntoEntity,
fn with_enum_wildcard<T: ComponentType<Enum> + ComponentId>(
&mut self,
) -> &mut Selfwhere
(Id<T>, u64): InternalIntoEntity,
set term with enum wildcard
Source§fn without<T>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
fn without<T>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
set term without Id
Source§fn without_enum<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>(
&mut self,
value: T,
) -> &mut Self
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 Selfwhere
(Id<T>, u64): InternalIntoEntity,
fn without_enum_wildcard<T: ComponentId + ComponentType<Enum> + EnumComponentInfo>(
&mut self,
) -> &mut Selfwhere
(Id<T>, u64): InternalIntoEntity,
set term without enum wildcard
Source§fn term_at_type<T: ComponentId>(&mut self) -> &mut Self
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
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
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 Selfwhere
Access: FromAccessArg<T>,
fn write<T>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
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
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 Selfwhere
Access: FromAccessArg<T>,
fn read<T>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
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
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
fn scope_open(&mut self) -> &mut Self
Open a scope for the query
Source§fn scope_close(&mut self) -> &mut Self
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 Selfwhere
T: ComponentId,
Self: QueryBuilderImpl<'a>,
fn order_by<T>(&mut self, compare: impl OrderByFn<T>) -> &mut Selfwhere
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
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
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
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
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 moreSource§fn on_group_create(&mut self, action: ecs_group_create_action_t) -> &mut Self
fn on_group_create(&mut self, action: ecs_group_create_action_t) -> &mut Self
Specify the
on_group_create action. Read moreSource§fn on_group_delete(&mut self, action: ecs_group_delete_action_t) -> &mut Self
fn on_group_delete(&mut self, action: ecs_group_delete_action_t) -> &mut Self
Specify the
on_group_delete action. Read moreSource§impl<'a, P, T> SystemAPI<'a, P, T> for ObserverBuilder<'a, P, T>where
T: QueryTuple,
P: ComponentId,
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
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>>::BuiltTypewhere
Func: FnMut(T::TupleType<'_>) + 'static,
fn each<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltTypewhere
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>>::BuiltTypewhere
Func: FnMut(T::TupleType<'_>) + 'static,
unsafe fn each_unchecked<Func>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltTypewhere
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>>::BuiltTypewhere
Func: FnMut(T::TupleType<'_>) + 'static,
fn each_internal<Func, const CHECKED: bool>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltTypewhere
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>>::BuiltTypewhere
Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,
fn each_entity<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltTypewhere
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>>::BuiltTypewhere
Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,
unsafe fn each_entity_unchecked<Func>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltTypewhere
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>>::BuiltTypewhere
Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,
fn each_entity_internal<Func, const CHECKED: bool>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltTypewhere
Func: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,
Internal implementation for both checked and unchecked
each_entity iteration.Source§unsafe fn each_iter_unchecked<Func>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltType
unsafe fn each_iter_unchecked<Func>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
Unchecked version of
each_iter that bypasses mutable aliasing checks. Read moreSource§fn each_iter_internal<Func, const CHECKED: bool>(
&mut self,
func: Func,
) -> <Self as Builder<'a>>::BuiltType
fn each_iter_internal<Func, const CHECKED: bool>( &mut self, func: Func, ) -> <Self as Builder<'a>>::BuiltType
Source§fn run<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
fn run<Func>(&mut self, func: Func) -> <Self as Builder<'a>>::BuiltType
Run iterator. This operation expects manual iteration over the tables with
iter.next() and iter.iter(). Read moreSource§fn run_each<Func, FuncEach>(
&mut self,
func: Func,
func_each: FuncEach,
) -> <Self as Builder<'a>>::BuiltType
fn run_each<Func, FuncEach>( &mut self, func: Func, func_each: FuncEach, ) -> <Self as Builder<'a>>::BuiltType
Run iterator with each forwarding. This operation expects manual
iteration over the tables with
iter.next() and iter.each() Read moreSource§fn run_each_entity<Func, FuncEachEntity>(
&mut self,
func: Func,
func_each_entity: FuncEachEntity,
) -> <Self as Builder<'a>>::BuiltTypewhere
Func: FnMut(TableIter<'_, true, P>) + 'static,
FuncEachEntity: FnMut(EntityView<'_>, T::TupleType<'_>) + 'static,
fn run_each_entity<Func, FuncEachEntity>(
&mut self,
func: Func,
func_each_entity: FuncEachEntity,
) -> <Self as Builder<'a>>::BuiltTypewhere
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 morefn run_each_iter<Func, FuncEachIter>(
&mut self,
func: Func,
func_each_iter: FuncEachIter,
) -> <Self as Builder<'a>>::BuiltTypewhere
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>
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,
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)
fn init_term_from<T: ComponentOrPairId>(&mut self)
initialize a new term from a component or pair Read more
Source§fn set_id(&mut self, id: impl IntoEntity) -> &mut Self
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 moreSource§fn entity(&mut self, entity: impl Into<Entity>) -> &mut Self
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 moreSource§fn set_var(&mut self, var_name: &'a str) -> &mut Self
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 first(&mut self) -> &mut Self
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
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 Selfwhere
Access: FromAccessArg<T>,
fn set_src<T: SingleAccessArg>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
Select src identifier Read more
Source§fn set_first<Q: SingleAccessArg>(&mut self, id: Q) -> &mut Selfwhere
Access: FromAccessArg<Q>,
fn set_first<Q: SingleAccessArg>(&mut self, id: Q) -> &mut Selfwhere
Access: FromAccessArg<Q>,
Select first identifier Read more
Source§fn set_second<T: SingleAccessArg>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
fn set_second<T: SingleAccessArg>(&mut self, id: T) -> &mut Selfwhere
Access: FromAccessArg<T>,
Select second identifier Read more
Source§fn up(&mut self) -> &mut Self
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 up_id(&mut self, traverse_relationship: impl IntoEntity) -> &mut Self
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 moreSource§fn cascade(&mut self) -> &mut Self
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
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 moreSource§fn desc(&mut self) -> &mut Self
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
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 set_inout_kind(&mut self, inout: InOutKind) -> &mut Self
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
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
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 moreSource§fn read_curr(&mut self) -> &mut Self
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 moreSource§fn read_write_curr(&mut self) -> &mut Self
fn read_write_curr(&mut self) -> &mut Self
Source§fn set_inout_none(&mut self) -> &mut Self
fn set_inout_none(&mut self) -> &mut Self
short for
set_inout(InOutKind::None) Read moreSource§impl<'a, P, T: QueryTuple> WorldProvider<'a> for ObserverBuilder<'a, P, T>
impl<'a, P, T: QueryTuple> WorldProvider<'a> for ObserverBuilder<'a, P, T>
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>
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> 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