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

State change taking place after its stage #4308

Closed
lukors opened this issue Mar 23, 2022 · 2 comments
Closed

State change taking place after its stage #4308

lukors opened this issue Mar 23, 2022 · 2 comments
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior

Comments

@lukors
Copy link
Contributor

lukors commented Mar 23, 2022

Bevy version

0.6.1

Operating system & version

Ubuntu 20.04.3 LTS

What you did

  • An exclusive system adds Res<MyResource> at the start of CoreStage::Update, and then an exclusive system removes it at the end of the stage.
  • input_system changes MyState to MyState::Two when you click the left mouse button.
  • using_system runs in CoreStage::Update when MyState::Two is entered, and has Res<MyResource> as an argument.

Run the following code and left click.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_state_to_stage(CoreStage::Update, MyState::One)
        .add_system_set_to_stage(
            CoreStage::Update,
            SystemSet::new()
                .with_system(inserting_system.exclusive_system().at_start())
                .with_system(removing_system.exclusive_system().at_end())
                .with_system(input_system)
                .with_system(using_system.with_run_criteria(State::on_enter(MyState::Two))),
        )
        .run();
}

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
enum MyState {
    One,
    Two,
}

struct MyResource;

fn inserting_system(world: &mut World) {
    world.insert_resource(MyResource);
}

fn removing_system(world: &mut World) {
    world.remove_resource::<MyResource>();
}

fn input_system(i_mouse_button: Res<Input<MouseButton>>, mut my_state: ResMut<State<MyState>>) {
    if i_mouse_button.just_pressed(MouseButton::Left) {
        my_state.set(MyState::Two);
    }
}

fn using_system(_my_resource: Res<MyResource>) {}

What you expected to happen

No crash, because the exclusive system that removes the Res<MyResource> is set to run at the end of the stage, which should be after state transitions.

What actually happened

Crash because it can't find the Res<MyResource>.

More info

Workaround: Insert it at the beginning of the next stage instead of at the end of this one.

@lukors lukors added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Mar 23, 2022
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events and removed S-Needs-Triage This issue needs to be labelled labels Mar 23, 2022
@alice-i-cecile
Copy link
Member

Added to the list in #2801.

@alice-i-cecile
Copy link
Member

Fixed by #7267.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior
Projects
Archived in project
Development

No branches or pull requests

2 participants