From 84ca612dcec6134aa047f6edbc9e846c7af9c18d Mon Sep 17 00:00:00 2001 From: akimakinai <105044389+akimakinai@users.noreply.github.com> Date: Thu, 5 Sep 2024 03:31:19 +0900 Subject: [PATCH] Fix `load_resource` stops proceeding if any of asset loading failed (#293) --- src/asset_tracking.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/asset_tracking.rs b/src/asset_tracking.rs index 265e712..f61992a 100644 --- a/src/asset_tracking.rs +++ b/src/asset_tracking.rs @@ -1,5 +1,7 @@ //! A high-level way to load collections of asset handles as resources. +use std::collections::VecDeque; + use bevy::prelude::*; pub(super) fn plugin(app: &mut App) { @@ -22,12 +24,14 @@ impl LoadResource for App { let assets = world.resource::(); let handle = assets.add(value); let mut handles = world.resource_mut::(); - handles.waiting.push((handle.untyped(), |world, handle| { - let assets = world.resource::>(); - if let Some(value) = assets.get(handle.id().typed::()) { - world.insert_resource(value.clone()); - } - })); + handles + .waiting + .push_back((handle.untyped(), |world, handle| { + let assets = world.resource::>(); + if let Some(value) = assets.get(handle.id().typed::()) { + world.insert_resource(value.clone()); + } + })); self } } @@ -37,7 +41,9 @@ type InsertLoadedResource = fn(&mut World, &UntypedHandle); #[derive(Resource, Default)] struct ResourceHandles { - waiting: Vec<(UntypedHandle, InsertLoadedResource)>, + // Use a queue for waiting assets so they can be cycled through and moved to + // `finished` one at a time. + waiting: VecDeque<(UntypedHandle, InsertLoadedResource)>, finished: Vec, } @@ -45,12 +51,12 @@ fn load_resource_assets(world: &mut World) { world.resource_scope(|world, mut resource_handles: Mut| { world.resource_scope(|world, assets: Mut| { for _ in 0..resource_handles.waiting.len() { - let (handle, insert_fn) = resource_handles.waiting.pop().unwrap(); + let (handle, insert_fn) = resource_handles.waiting.pop_front().unwrap(); if assets.is_loaded_with_dependencies(&handle) { insert_fn(world, &handle); resource_handles.finished.push(handle); } else { - resource_handles.waiting.push((handle, insert_fn)); + resource_handles.waiting.push_back((handle, insert_fn)); } } });