bevyengine / bevyengine/bevy

Unable to access Window after AppExit has posted

Open
#6,323 0 comments 0 reactions 0 assignees View on GitHub
A-Windowing C-Usability
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

# Problem
There is no way to access windows from Res when the app is exiting.

## Use Case
The use case for this is to save window size/position data for next time the app runs. If someone using mutliple windows (as seen in the game [Angband](http://angband.oook.cz/forum/showthread.php?s=6ea835540dcb8a240400d93934afae41&t=2689)) Getting each of these windows and copying their data each tick seems like it may become non-trivial.

## Workaround
The current work around is to access the window(s) on every frame and write the information on exit. For example:

```
fn on_shutdown(
mut event: EventReader,
windows: Res,
q_main_camera: Query<&Camera, With>,
mut window_resource: ResMut,
) {
// SKIP ALL CODE UNTIL EXITING
//if event.iter().len() > 0 {

if let Ok(camera) = q_main_camera.get_single() {
// THIS RETURNS "None"
if let Some(window) = if let RenderTarget::Window(id) = camera.target {
windows.get(id)
} else {
windows.get_primary()
} {
if window.mode() == WindowMode::Windowed {
window_resource.data = WindowData::Windowed(window.width() as u16, window.height() as u16);
} else {
window_resource.data = WindowData::Fullscreen;
}
} else {
// THIS PRINTS WHEN EXITING
println!("No window!");
}
}

// RUN ABOVE CODE EACH FRAME
if event.iter().len() > 0 {
window_resource.data.write();
}
}
```

## Solution
Delay window destruction until after any chance a system would use it.

Solving #6215 *may* solve this as well as AppExit could fire earlier in the tick and the windows may still be available, but I doubt the window destruction is happening in the middle of a tick which would make this a separate issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.