Panic on failure in scene example (#17812)

# Objective

Fixes #17810

Y'all picked the option I already implemented, yay.

## Solution

Add a system that panics if the load state of an asset is `Failed`.

## Testing

`cargo run --example scene`

- Tested with valid scene file
- Introduced a syntax error in the scene file
- Deleted the scene file
This commit is contained in:
Rob Parrett 2025-02-11 16:11:27 -07:00 committed by GitHub
parent 94deca81bf
commit 7398d33b79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@
//! won't work on WASM because WASM typically doesn't have direct filesystem access. //! won't work on WASM because WASM typically doesn't have direct filesystem access.
//! //!
use bevy::{prelude::*, tasks::IoTaskPool}; use bevy::{asset::LoadState, prelude::*, tasks::IoTaskPool};
use core::time::Duration; use core::time::Duration;
use std::{fs::File, io::Write}; use std::{fs::File, io::Write};
@ -42,7 +42,7 @@ fn main() {
Startup, Startup,
(save_scene_system, load_scene_system, infotext_system), (save_scene_system, load_scene_system, infotext_system),
) )
.add_systems(Update, log_system) .add_systems(Update, (log_system, panic_on_fail))
.run(); .run();
} }
@ -226,3 +226,13 @@ fn infotext_system(mut commands: Commands) {
}, },
)); ));
} }
/// To help with Bevy's automated testing, we want the example to close with an appropriate if the
/// scene fails to load. This is most likely not something you want in your own app.
fn panic_on_fail(scenes: Query<&DynamicSceneRoot>, asset_server: Res<AssetServer>) {
for scene in &scenes {
if let Some(LoadState::Failed(err)) = asset_server.get_load_state(&scene.0) {
panic!("Failed to load scene. {}", err);
}
}
}