Crates.io | bevy_mod_reaction |
lib.rs | bevy_mod_reaction |
version | 0.2.0-alpha.1 |
source | src |
created_at | 2024-11-05 01:05:37.588672 |
updated_at | 2024-11-05 01:53:54.499548 |
description | Reactive components for Bevy |
homepage | |
repository | |
max_upload_size | |
id | 1435882 |
size | 139,869 |
Reactive components for Bevy.
A Reaction
is a component around a ReactiveSystem
, which runs every time its parameters have changed. Bevy's built-in change detection mechanisms are used to efficiently react to changes in state.
/// This reaction will only run if a `Damage` component is changed.
commands.spawn(Reaction::new(|_: In<Scope>, query: Query<&Damage>| {
for dmg in &query {
dbg!(dmg.0);
}
}));
For coarse-grained reactivity ReactiveQuery
tracks the entities read and only re-runs the current system if those values have changed. Bundles of components can also be derived:
// Coarse-grained reactivity:
// This reaction will only run when the `Health` component belonging to `scope.entity` changes.
commands.spawn((
Health(100),
Reaction::derive(|scope: In<Scope>, mut query: ReactiveQuery<&Health>| {
let health = query.get(scope.entity).unwrap();
Damage(health.0 * 2)
}),
));
Switch statements are also supported, with more primitives coming soon
commands.spawn((
Health(0),
Reaction::switch(
|scope: In<Scope>, query: ReactiveQuery<&Health>| {
let health = query.get(scope.entity).unwrap();
health.0 == 0
},
|| Armor(50),
|| Damage(100),
),
));