bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Resources inserted during `Startup` schedule are not available in `OnEnter` schedule.

Open CooCooCaCha opened this issue 1 year ago • 1 comments

Bevy version

0.14

What you did

Insert a resource during Startup and try to use it in OnEnter.

What went wrong

Bevy crashes with Resource requested does not exist.

Additional information

Perhaps commands aren't being flushed properly before OnEnter? It seems to work during Update.

Minimal repro:

use bevy::prelude::*;

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

#[derive(Resource)]
struct MyResource {
    value: String,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .init_state::<AppState>()
        .add_systems(Startup, setup)
        .add_systems(OnEnter(AppState::Menu), enter_menu_state)
        .run();
}

fn setup(mut commands: Commands) {
    commands.insert_resource(MyResource {
        value: "Hello, Bevy!".to_string(),
    });
}

fn enter_menu_state(my_resource: Res<MyResource>) {
    println!("Entered menu state {}", my_resource.value);
}

CooCooCaCha avatar Jul 07 '24 16:07 CooCooCaCha

My take on how this (and the related issues) should be fixed for bevy 0.15: https://github.com/bevyengine/bevy/issues/13968#issuecomment-2185259558

benfrankel avatar Jul 08 '24 03:07 benfrankel