bevy
bevy copied to clipboard
Resources inserted during `Startup` schedule are not available in `OnEnter` schedule.
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);
}
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