Skip to content

Commit

Permalink
Fix load_resource stops proceeding if any of asset loading failed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
akimakinai authored Sep 4, 2024
1 parent 07a0ce0 commit 84ca612
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/asset_tracking.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -22,12 +24,14 @@ impl LoadResource for App {
let assets = world.resource::<AssetServer>();
let handle = assets.add(value);
let mut handles = world.resource_mut::<ResourceHandles>();
handles.waiting.push((handle.untyped(), |world, handle| {
let assets = world.resource::<Assets<T>>();
if let Some(value) = assets.get(handle.id().typed::<T>()) {
world.insert_resource(value.clone());
}
}));
handles
.waiting
.push_back((handle.untyped(), |world, handle| {
let assets = world.resource::<Assets<T>>();
if let Some(value) = assets.get(handle.id().typed::<T>()) {
world.insert_resource(value.clone());
}
}));
self
}
}
Expand All @@ -37,20 +41,22 @@ 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<UntypedHandle>,
}

fn load_resource_assets(world: &mut World) {
world.resource_scope(|world, mut resource_handles: Mut<ResourceHandles>| {
world.resource_scope(|world, assets: Mut<AssetServer>| {
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));
}
}
});
Expand Down

0 comments on commit 84ca612

Please sign in to comment.