From 7398d33b79beeccf9a3cbab1d67d00b7a96a1904 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Tue, 11 Feb 2025 16:11:27 -0700 Subject: [PATCH] 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 --- examples/scene/scene.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 58d733a80c..e0072df170 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -24,7 +24,7 @@ //! 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 std::{fs::File, io::Write}; @@ -42,7 +42,7 @@ fn main() { Startup, (save_scene_system, load_scene_system, infotext_system), ) - .add_systems(Update, log_system) + .add_systems(Update, (log_system, panic_on_fail)) .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) { + for scene in &scenes { + if let Some(LoadState::Failed(err)) = asset_server.get_load_state(&scene.0) { + panic!("Failed to load scene. {}", err); + } + } +}