
# Objective As discussed in #14275, Bevy is currently too prone to panic, and makes the easy / beginner-friendly way to do a large number of operations just to panic on failure. This is seriously frustrating in library code, but also slows down development, as many of the `Query::single` panics can actually safely be an early return (these panics are often due to a small ordering issue or a change in game state. More critically, in most "finished" products, panics are unacceptable: any unexpected failures should be handled elsewhere. That's where the new With the advent of good system error handling, we can now remove this. Note: I was instrumental in a) introducing this idea in the first place and b) pushing to make the panicking variant the default. The introduction of both `let else` statements in Rust and the fancy system error handling work in 0.16 have changed my mind on the right balance here. ## Solution 1. Make `Query::single` and `Query::single_mut` (and other random related methods) return a `Result`. 2. Handle all of Bevy's internal usage of these APIs. 3. Deprecate `Query::get_single` and friends, since we've moved their functionality to the nice names. 4. Add detailed advice on how to best handle these errors. Generally I like the diff here, although `get_single().unwrap()` in tests is a bit of a downgrade. ## Testing I've done a global search for `.single` to track down any missed deprecated usages. As to whether or not all the migrations were successful, that's what CI is for :) ## Future work ~~Rename `Query::get_single` and friends to `Query::single`!~~ ~~I've opted not to do this in this PR, and smear it across two releases in order to ease the migration. Successive deprecations are much easier to manage than the semantics and types shifting under your feet.~~ Cart has convinced me to change my mind on this; see https://github.com/bevyengine/bevy/pull/18082#discussion_r1974536085. ## Migration guide `Query::single`, `Query::single_mut` and their `QueryState` equivalents now return a `Result`. Generally, you'll want to: 1. Use Bevy 0.16's system error handling to return a `Result` using the `?` operator. 2. Use a `let else Ok(data)` block to early return if it's an expected failure. 3. Use `unwrap()` or `Ok` destructuring inside of tests. The old `Query::get_single` (etc) methods which did this have been deprecated.
60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
//! a test that confirms that 'bevy' does not panic while changing from Windowed to SizedFullscreen when viewport is set
|
|
|
|
use bevy::{prelude::*, render::camera::Viewport, window::WindowMode};
|
|
|
|
//Having a viewport set to the same size as a window used to cause panic on some occasions when switching to SizedFullscreen
|
|
const WINDOW_WIDTH: f32 = 1366.0;
|
|
const WINDOW_HEIGHT: f32 = 768.0;
|
|
|
|
fn main() {
|
|
//Specify Window Size.
|
|
let window = Window {
|
|
resolution: (WINDOW_WIDTH, WINDOW_HEIGHT).into(),
|
|
..default()
|
|
};
|
|
let primary_window = Some(window);
|
|
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window,
|
|
..default()
|
|
}))
|
|
.add_systems(Startup, startup)
|
|
.add_systems(Update, toggle_window_mode)
|
|
.run();
|
|
}
|
|
|
|
fn startup(mut cmds: Commands) {
|
|
//Match viewport to Window size.
|
|
let physical_position = UVec2::new(0, 0);
|
|
let physical_size = Vec2::new(WINDOW_WIDTH, WINDOW_HEIGHT).as_uvec2();
|
|
let viewport = Some(Viewport {
|
|
physical_position,
|
|
physical_size,
|
|
..default()
|
|
});
|
|
|
|
cmds.spawn(Camera2d).insert(Camera {
|
|
viewport,
|
|
..default()
|
|
});
|
|
}
|
|
|
|
fn toggle_window_mode(mut qry_window: Query<&mut Window>) {
|
|
let Ok(mut window) = qry_window.single_mut() else {
|
|
return;
|
|
};
|
|
|
|
window.mode = match window.mode {
|
|
WindowMode::Windowed => {
|
|
//it takes a while for the window to change from windowed to sizedfullscreen and back
|
|
std::thread::sleep(std::time::Duration::from_secs(4));
|
|
WindowMode::SizedFullscreen
|
|
}
|
|
_ => {
|
|
std::thread::sleep(std::time::Duration::from_secs(4));
|
|
WindowMode::Windowed
|
|
}
|
|
};
|
|
}
|