jakobhellermann / bevy-inspector-egui

Inspector plugin for the bevy game engine
Apache License 2.0
1.12k stars 166 forks source link

WorldInspectorGui panics if no PrimaryWindow is present #131

Closed MWFIAE closed 1 year ago

MWFIAE commented 1 year ago

If you configure the WindowPlugin to not close the app if the PrimaryWindow is closed the world inspector gui will panic as it can't retrieve the window.

Configuring the WindowPlugin:

    .add_plugins(DefaultPlugins.set(WindowPlugin{
        exit_condition: ExitCondition::DontExit,
        ..Default::default()
    }))

Panic after closing the window:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<&bevy_egui::EguiContext, bevy_ecs::query::filter::With<bevy_window::window::PrimaryWindow>>")', C:\<redacted>\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy-inspector-egui-0.18.3\src\quick.rs:80:10

This should be a fairly simple fix, the existing code looks like this:

fn world_inspector_ui(world: &mut World) {
    let mut egui_context = world
        .query_filtered::<&mut EguiContext, With<PrimaryWindow>>()
        .single(world)
        .clone();
    egui::Window::new("World Inspector")
        .default_size(DEFAULT_SIZE)
        .show(egui_context.get_mut(), |ui| {
            egui::ScrollArea::vertical().show(ui, |ui| {
                bevy_inspector::ui_for_world(world, ui);
                ui.allocate_space(ui.available_size());
            });
        });
}

Replacing single with get_single would return a result which can be properly checked without panic :)