Skip to content

Commit 80028d1

Browse files
Fix error when closing window in 2d_viewport_to_world example (#14804)
# Objective - Fix error when closing window in 2d_viewport_to_world example. Before ``` 2024-08-17T22:51:47.690252Z INFO bevy_winit::system: Creating new window "App" (0v1#4294967296) 2024-08-17T22:52:22.062959Z INFO bevy_window::system: No windows are open, exiting 2024-08-17T22:52:22.064045Z INFO bevy_winit::system: Closing window 0v1#4294967296 thread 'Compute Task Pool (5)' panicked at examples/2d/2d_viewport_to_world.rs:20:41: called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<&bevy_window::window::Window>") ``` After ``` 2024-08-17T22:57:31.623499Z INFO bevy_winit::system: Creating new window "App" (0v1#4294967296) 2024-08-17T22:57:32.990058Z INFO bevy_window::system: No windows are open, exiting 2024-08-17T22:57:32.991152Z INFO bevy_winit::system: Closing window 0v1#4294967296 2024-08-17T22:57:32.994426Z INFO bevy_window::system: No windows are open, exiting * Terminal will be reused by tasks, press any key to close it. ``` ## Solution - Check if the window still exists before drawing the cursor
1 parent a6d2339 commit 80028d1

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

examples/2d/2d_viewport_to_world.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ fn draw_cursor(
1717
) {
1818
let (camera, camera_transform) = camera_query.single();
1919

20-
let Some(cursor_position) = windows.single().cursor_position() else {
20+
let Ok(window) = windows.get_single() else {
21+
return;
22+
};
23+
24+
let Some(cursor_position) = window.cursor_position() else {
2125
return;
2226
};
2327

0 commit comments

Comments
 (0)