1

I am unsure whether I am confusing what the Bevy's Change Detection systems do , or if I am just doing it wrong. But, in attempts to detect changes, I am only notified when the component is spawned.

Below, I spawn a tuple of Group,Size(2). Then, sizequery is run each frame until the size is 5. In this time, groupsize only runs once, at size 2. Why is this?

use bevy:: {
    prelude::*,
};

fn main () {
    App::build()
        .add_resource(WindowDescriptor {
            ..Default::default()
        })
        .add_default_plugins()
        .add_startup_system(setup.system())
        .add_plugin(TestPlugin)
        .run();
}

struct Group;
struct Size(u32);

fn setup(mut commands: Commands) {
    commands.spawn((Group,Size(2)));
}

fn groupsize(mut query: Query<Changed<Size>>) {
    for s in &mut query.iter() {
        println!("GS-{}",s.0);
    }
}

fn sizequery(mut query: Query<&mut Size>) {
    for mut s in &mut query.iter() {
        if s.0 < 5 {
            s.0 = s.0+1;
            println!("add{}",s.0);
        }
    }
}

pub struct TestPlugin;

impl Plugin for TestPlugin {
    fn build(&self, app: &mut AppBuilder) {
        app.add_system(groupsize.system());
        app.add_system(sizequery.system());
    }
}

Cargo.toml:

[package]
name = "bevy_test"
version = "0.1.0"
edition = "2018"

[dependencies]
bevy = "0.1.3"
STF_ZBR
  • 617
  • 1
  • 4
  • 19

1 Answers1

2

Its because sizequery seems to run after groupsize.
I might be wrong there but I got your code to run to what I believe is your intention.
I changed your plugin to:

fn build(&self, app: &mut AppBuilder) {
    app.add_system(groupsize.system())
        .add_system_to_stage(stage::FIRST, sizequery.system());
}

groupsize will not print GS-2 since sizequery updates its value before it can. The output from the changes:

add3
GS-3
add4
GS-4
add5
GS-5

So the answer to your written question would be, systems that read changes should be set to stages that run after systems that make the change.

Will
  • 1,059
  • 2
  • 10
  • 22
  • This solves the issue at-hand. I want to add, it also seems to work if the order in which the systems are added is changed. If `sizequery` is added before `groupsize`, then it works as expected. – STF_ZBR Sep 09 '20 at 23:13