pub struct PropertyChangeTrigger<E, P>{ /* private fields */ }Expand description
Trigger criterion for writes to an entity property with particular previous and/or current values.
PropertyChangeTrigger observes
PropertyChangeEvent for a specific
entity/property pair and emits when a property write matches its configured previous value,
current value, or both.
§Construction
PropertyChangeTrigger::<E, P>::from(from)
PropertyChangeTrigger::<E, P>::to(to)
PropertyChangeTrigger::<E, P>::from_to(from, to)
PropertyChangeTrigger::<E, P>::from(from).once()
PropertyChangeTrigger::<E, P>::from(from).repeating()§Observation
The observation data passed to
TriggerCriterion::emit_with is
PropertyChangeTriggerEvent. It contains the entity ID, the previous property value, the
current property value, and the selected TriggerMode with which the
trigger was created:
pub struct PropertyChangeTriggerEvent<E, P>
where
E: Entity,
P: Property<E>,
{
pub entity_id: EntityId<E>,
pub previous: P,
pub current: P,
pub mode: TriggerMode,
}§Semantics
By default, the criterion uses TriggerMode::Repeating and
emits for every matching property write. Call PropertyChangeTrigger::once to emit only for
the first matching write, or PropertyChangeTrigger::repeating to return to the default
repeating behavior.
A from constraint matches event.previous; a to constraint matches event.current;
from_to requires both. Property writes are eventful even when the old and new values are
equal. For example, PropertyChangeTrigger::to(Alive(false)) can match a write that sets
Alive(false) when the entity was already Alive(false), and
PropertyChangeTrigger::from_to(Alive(false), Alive(false)) matches that no-op write exactly.
§Example
use ixa::{Context, ContextEntitiesExt, define_entity, define_property, IxaEvent};
use ixa::entity::EntityId;
use ixa::triggers::{ContextTriggersExt, PropertyChangeTrigger, TriggerCriterion};
define_entity!(Person);
define_property!(struct Alive(bool), Person, default_const = Alive(true));
#[derive(IxaEvent)]
struct FirstDeath {
person: EntityId<Person>
}
let mut context = Context::new();
context.register_trigger(
PropertyChangeTrigger::from_to(Alive(true), Alive(false))
.once()
.emit_with(|observation| FirstDeath {
person: observation.entity_id
}),
);
context.subscribe_to_event(|_context, _event: FirstDeath| {
// respond when a person changes from alive to dead
});Implementations§
Trait Implementations§
Source§impl<E, P> TriggerCriterion for PropertyChangeTrigger<E, P>
impl<E, P> TriggerCriterion for PropertyChangeTrigger<E, P>
Source§type Observation = PropertyChangeTriggerEvent<E, P>
type Observation = PropertyChangeTriggerEvent<E, P>
Source§fn install<F>(self, context: &mut Context, on_match: F)
fn install<F>(self, context: &mut Context, on_match: F)
context.