mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
it's a followup of #1550 I think calling explicit methods/values instead of default makes the code easier to read: "what is `Quat::default()`" vs "Oh, it's `Quat::IDENTITY`" `Transform::identity()` and `GlobalTransform::identity()` can also be consts and I replaced the calls to their `default()` impl with `identity()`
45 lines
1.4 KiB
Rust
45 lines
1.4 KiB
Rust
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::identity(),));
|
|
}
|
|
|
|
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,);
|
|
}
|
|
}
|