Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Flags<T> as a query to get flags of component #1172

Merged
merged 5 commits into from
Dec 31, 2020

Conversation

mockersf
Copy link
Member

@mockersf mockersf commented Dec 31, 2020

This let user add Flags<MyComponent> in a query to get the component flags for an entity

I have an example, but didn't commit it as I'm not sure it's a feature that need to be put forward in an example

view example

use bevy::prelude::*;
use rand::Rng;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup.system())
        .add_system(change_component.system())
        .add_system(change_detection.system())
        .run();
}

#[derive(Debug)]
struct MyComponent(f64);

fn setup(commands: &mut Commands) {
    commands.spawn((MyComponent(0.),));
}

fn change_component(time: Res<Time>, mut query: Query<&mut MyComponent>) {
    for mut component in query.iter_mut() {
        if rand::thread_rng().gen_bool(0.1) {
            info!("changing component");
            component.0 = time.seconds_since_startup();
        }
    }
}
fn change_detection(query: Query<(&MyComponent, Flags<MyComponent>)>) {
    for (component, flags) in query.iter() {
        info!(
            "{:?} -> added: {:?} - mutated: {:?} - changed: {:?}",
            component,
            flags.added(),
            flags.mutated(),
            flags.changed()
        );
    }
}

and it logs:

Dec 31 02:28:28.640  INFO flags: MyComponent(0.0) -> added: true - mutated: false - changed: true
Dec 31 02:28:28.773  INFO flags: MyComponent(0.0) -> added: false - mutated: false - changed: false
Dec 31 02:28:28.876  INFO flags: MyComponent(0.0) -> added: false - mutated: false - changed: false
Dec 31 02:28:28.889  INFO flags: changing component
Dec 31 02:28:28.889  INFO flags: MyComponent(0.471366423) -> added: false - mutated: true - changed: true
Dec 31 02:28:28.910  INFO flags: MyComponent(0.471366423) -> added: false - mutated: false - changed: false
Dec 31 02:28:28.979  INFO flags: MyComponent(0.471366423) -> added: false - mutated: false - changed: false
Dec 31 02:28:28.993  INFO flags: changing component
Dec 31 02:28:28.993  INFO flags: MyComponent(0.575871597) -> added: false - mutated: true - changed: true
Dec 31 02:28:29.004  INFO flags: MyComponent(0.575871597) -> added: false - mutated: false - changed: false

@mockersf mockersf changed the title add Flags as a query to get flags of component add Flags<T> as a query to get flags of component Dec 31, 2020
@TheRawMeatball
Copy link
Member

Personally, I think its best to keep the examples. There isn't much in terms of documentation, so without an example almost no one would even know the feature exists.

@mockersf
Copy link
Member Author

as there wasn't an example on Changed<T>, I added it also

@cart
Copy link
Member

cart commented Dec 31, 2020

This looks good to me!

@cart cart merged commit d91117d into bevyengine:master Dec 31, 2020
@mockersf mockersf deleted the query-flags branch April 27, 2021 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants