#State plugin system is being called when state is not set to that value

2 messages · Page 1 of 1 (latest)

tidal trail
#

When I run this program, which you will see is initialized with AppState::Menu, the on_enter system set for SimulationPlugin is correctly not called (does not output "Entering simulation") until I change the AppState to AppState::Simulation, however, the App continuously prints out "Running Simulation" before (incorrectly) and after the state is switched... What do I need to do to make it only run the SimulationPlugin on_update system set when the AppState is AppState::Simulation:

main.ts

use bevy::prelude::*;

mod menu;
mod simulation;

pub mod utils;

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
enum AppState {
  Splash,
  Menu,
  Designer,
  Simulation,
}

fn setup_camera(mut commands: Commands) {
  commands.spawn_bundle(Camera2dBundle::default());
}

fn setup(commands: Commands) {
  setup_camera(commands);
}

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_state(AppState::Menu)
    .add_startup_system(setup)
    .add_plugin(menu::MenuPlugin)
    .add_plugin(simulation::SimulationPlugin)
    .run();
}

simulation.rs

use bevy::prelude::*;
use bevy::time::FixedTimestep;

use super::{utils, AppState};

pub struct SimulationPlugin;

#[derive(Component)]
struct OnSimulation;

impl Plugin for SimulationPlugin {
  fn build(&self, app: &mut App) {
    app.add_system_set(SystemSet::on_enter(AppState::Simulation).with_system(setup))
      .add_system_set(
        SystemSet::on_update(AppState::Simulation)
          .with_run_criteria(FixedTimestep::step(1.))
          .with_system(run_every_second),
      )
      .add_system_set(
        SystemSet::on_exit(AppState::Simulation)
          .with_system(utils::despawn_screen::<OnSimulation>),
      );
  }
}

fn setup() {
  println!("Entering simulation");
}

fn run_every_second() {
  println!("Running simulation");
}
#

It would appear that the .with_run_criteria is overriding the condition that it should only be run on_update(AppState::Simulation), and is running it every one second, regardless of AppState