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

[Bug?] State is not updated when manually updating App #13874

Closed
not-elm opened this issue Jun 16, 2024 · 7 comments · Fixed by #13877
Closed

[Bug?] State is not updated when manually updating App #13874

not-elm opened this issue Jun 16, 2024 · 7 comments · Fixed by #13877
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@not-elm
Copy link

not-elm commented Jun 16, 2024

Bevy version

v0.14.0-rc.2

What you did

My library has a similar to the following test code, but it failed after I updated the bevy version from v0.13.2 to v0.14.0-rc2.

use bevy::prelude::*;

fn main() {
    let mut app = App::new();
    app.add_plugins(MinimalPlugins);
    app.init_resource::<StateChanged>();
    app.init_state::<GameState>();
    app.add_systems(Update, wait_state);
    app.update();
    assert!(!app.world().resource::<StateChanged>().0);
    app.insert_state(GameState::InGame);
    app.update();
    // panic!! 
    assert!(app.world().resource::<StateChanged>().0);
}

#[derive(Resource, Default, Debug)]
struct StateChanged(bool);

#[derive(Eq, PartialEq, Debug, Default, Copy, Clone, Hash, States)]
enum GameState {
    #[default]
    Title,
    InGame,
}

fn wait_state(
    mut changed: ResMut<StateChanged>,
    state: Res<State<GameState>>,
) {
    if matches!(state.get(), GameState::InGame) {
        changed.0 = true;
    }
}

What went wrong

Console output:

thread 'main' panicked at src\main.rs:19:5:
assertion failed: app.world().resource::<StateChanged>().0
@not-elm not-elm added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jun 16, 2024
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Jun 16, 2024
@alice-i-cecile
Copy link
Member

FYI @lee-orr @benfrankel @MiniaczQ

@lee-orr
Copy link
Contributor

lee-orr commented Jun 16, 2024

@alice-i-cecile - I believe the source of this is App::new() doesn't set up the main schedule, so StateTransition isn't added in.

Do you think a good solution here is documenting the need to set up that schedule when you aren't building on the main schedule, or ensuring it's run by default even if no meta-schedule has been added?

@alice-i-cecile
Copy link
Member

I agree with your diagnosis of the problem. IMO we should warn about this during .init_state / insert_state. Users won't read the docs for this sort of weirdness, but they will check for warnings.

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! D-Straightforward Simple bug fixes and API improvements, docs, test and examples and removed C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong labels Jun 16, 2024
@MiniaczQ
Copy link
Contributor

MiniaczQ commented Jun 16, 2024

I believe the source of this is App::new() doesn't set up the main schedule, so StateTransition isn't added in.

Not quite, StateTransition is no longer part of the default main schedule, it's added through StatesPlugin

Can the state installation methods check for plugin existence? That'd be the best scenario, we shouldn't implicitly install plugins.

@alice-i-cecile
Copy link
Member

is_plugin_added is the method you want :)

@MiniaczQ
Copy link
Contributor

Great, I'll have a fix in 10 min

@MiniaczQ
Copy link
Contributor

For the author, you want to add StatesPlugin manually when using MinimalPlugins

github-merge-queue bot pushed a commit that referenced this issue Jun 16, 2024
# Objective

- Fixes #13874

## Solution

- Confirm that the `StatesPlugin` is installed when trying to add
states.
- Skipped for state scoped entities, since those will warn about missing
states.
mockersf pushed a commit that referenced this issue Jun 19, 2024
# Objective

- Fixes #13874

## Solution

- Confirm that the `StatesPlugin` is installed when trying to add
states.
- Skipped for state scoped entities, since those will warn about missing
states.
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-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants