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

Hard lock using State across stages #1671

Closed
siler opened this issue Mar 16, 2021 · 8 comments
Closed

Hard lock using State across stages #1671

siler opened this issue Mar 16, 2021 · 8 comments
Labels
A-Core Common functionality for all bevy apps C-Bug An unexpected or incorrect behavior

Comments

@siler
Copy link

siler commented Mar 16, 2021

Bevy version

main: 284889c

Operating system & version

Windows 10 20H2

What you did

use bevy::{
    prelude::*,
    input::{
        keyboard::KeyboardInput,
        ElementState
    }
};

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_state(GameState::Start)
        .add_system_set(SystemSet::on_update(GameState::Start).with_system(transition_check.system()))
        .add_system_set_to_stage(CoreStage::PreUpdate, SystemSet::on_update(GameState::Freeze).with_system(do_something.system()))
        .run();
}

#[derive(Clone, Eq, PartialEq)]
pub enum GameState {
    Start,
    Freeze
}

fn transition_check(mut state: ResMut<State<GameState>>, mut keyboard_event_reader: EventReader<KeyboardInput>) {
    for event in keyboard_event_reader.iter() {
        if event.key_code == Some(KeyCode::Return) && event.state == ElementState::Pressed {
            state.set_next(GameState::Freeze).unwrap();
        }
    }
}

fn do_something() {}

What you expected to happen

When the return key is pressed the state transitions to GameState::Freeze and the do_something system begins firing.

What actually happened

When the return key is pressed the application hard locks and enters "not responding".

Additional information

If I try to use a state that I added with add_state(..) as a run criteria for a system I add to any stage other than update (via add_system_set_to_stage(..), the application freezes as soon as the state transition occurs.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Core Common functionality for all bevy apps labels Mar 16, 2021
@alice-i-cecile alice-i-cecile added this to the Bevy 0.5 milestone Mar 16, 2021
@rparrett
Copy link
Contributor

rparrett commented Mar 16, 2021

Able to reproduce on macos. It seems that the do_nothing system executes over and over (in the same frame?), but and no other systems do after the transition.

@mockersf
Copy link
Member

this freeze for me:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_state(GameState::Freeze)
        .add_system_set_to_stage(
            CoreStage::PreUpdate,
            SystemSet::on_update(GameState::Freeze).with_system(do_something.system()),
        )
        .run();
}

#[derive(Clone, Eq, PartialEq)]
pub enum GameState {
    Freeze,
}

fn do_something() {}

it does not freeze if the system set is added to stage CoreStage::Update

@mockersf
Copy link
Member

mockersf commented Mar 17, 2021

this is because the system set make_driver is needed to handle state correctly but is added only to stage CoreStage::Update

pub fn add_state<T: Component + Clone + Eq>(&mut self, initial: T) -> &mut Self {
self.insert_resource(State::new(initial))
.add_system_set(State::<T>::make_driver())
}

pub fn add_system_set(&mut self, system_set: SystemSet) -> &mut Self {
self.add_system_set_to_stage(CoreStage::Update, system_set)
}

@rparrett
Copy link
Contributor

Indeed, this patch to the code from the original issue seems to make things work properly

         .add_state(GameState::Start)
+        .add_state_to_stage(CoreStage::PreUpdate, GameState::Start)

@cart
Copy link
Member

cart commented Mar 25, 2021

Removing this from the 0.5 milestone in favor of #1746

@cart cart removed this from the Bevy 0.5 milestone Mar 25, 2021
bors bot pushed a commit that referenced this issue Mar 25, 2021
This is intended to help protect users against #1671. It doesn't resolve the issue, but I think its a good stop-gap solution for 0.5. A "full" fix would be very involved (and maybe not worth the added complexity).
@djeedai
Copy link
Contributor

djeedai commented Dec 4, 2021

Can confirm both bug and workaround from @rparrett (thanks!).

@james7132
Copy link
Member

Checking in, is this still relevant after stageless? States now work across base sets and the run conditions have significantly simplified the mental model for states.

@alice-i-cecile alice-i-cecile closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2023
@alice-i-cecile
Copy link
Member

This is no longer possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Core Common functionality for all bevy apps C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

7 participants