Skip to content

State change during on_exit causes infinite loop #4271

@hayashi-stl

Description

@hayashi-stl

Bevy version

0.6.1

Operating system & version

Ubuntu 20.04

What you did

I changed the state during a system that runs on exiting a state:

use bevy::prelude::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum AppState {
    A,
    B,
}

fn main() {
    App::new()
        .add_state(AppState::A)
        .add_system_set(SystemSet::on_update(AppState::A).with_system(change_state))
        .add_system_set(SystemSet::on_exit(AppState::A).with_system(change_state))
        .run();
}

fn change_state(mut state: ResMut<State<AppState>>) {
    println!("Changing state to B");
    state.set(AppState::B).unwrap();
}

What you expected to happen

A panic, probably, since either the state transition didn't happen yet (in which case there's a state transition waiting to happen, so set should panic), or the state transition did happen (in which case we're already in state B, so set should panic).

What actually happened

Changing state to B prints forever

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions