forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_detection.rs
45 lines (39 loc) · 1.44 KB
/
change_detection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use bevy::prelude::*;
use rand::Rng;
// This example illustrates how to react to component change
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(change_component.system())
.add_system(change_detection.system())
.add_system(flags_monitoring.system())
.run();
}
#[derive(Debug)]
struct MyComponent(f64);
fn setup(mut commands: Commands) {
commands.spawn((MyComponent(0.),));
commands.spawn((Transform::default(),));
}
fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)>) {
for (entity, mut component) in query.iter_mut() {
if rand::thread_rng().gen_bool(0.1) {
info!("changing component {:?}", entity);
component.0 = time.seconds_since_startup();
}
}
}
// There are query filters for `Changed<T>`, `Added<T>` and `Mutated<T>`
// Only entities matching the filters will be in the query
fn change_detection(query: Query<(Entity, &MyComponent), Changed<MyComponent>>) {
for (entity, component) in query.iter() {
info!("{:?} changed: {:?}", entity, component,);
}
}
// By looking at flags, the query is not filtered but the information is available
fn flags_monitoring(query: Query<(Entity, Option<&MyComponent>, Option<Flags<MyComponent>>)>) {
for (entity, component, flags) in query.iter() {
info!("{:?}: {:?} -> {:?}", entity, component, flags,);
}
}