You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this minimal example the app will crash, since neither bevy's AssetServer or bevy_asset_loader wait for asset dependencies. So AssetA gets loaded successfully, but its slow dependency may not be loaded when you enter InGame state.
use bevy::prelude::*;use bevy_asset_loader::*;use iyes_loopless::prelude::*;use anyhow::Error;use bevy::reflect::TypeUuid;use bevy::asset::{AssetLoaderasAssetLoaderTrait,LoadContext,BoxedFuture,LoadedAsset,Assets,AssetPath};#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]enumGameState{Boot,InGame,}// Imagine Asset A is some lightweight fast-to-load asset (like a config)#[derive(TypeUuid)]#[uuid="cc570c16-ffd0-11ec-b939-0242ac120003"]structAssetA;// And Asset B is some heavy slow-to-load asset (a quite big texture) #[derive(TypeUuid)]#[uuid="cc570c16-ffd0-11ec-b939-0242ac120002"]structAssetB;#[derive(Clone,Copy,Default)]structAssetALoader;implAssetLoaderTraitforAssetALoader{fnload<'a>(&'a self,_bytes:&'a [u8],load_context:&'a mutLoadContext) -> BoxedFuture<Result<(),Error>>{Box::pin(asyncmove{letmut asset = LoadedAsset::new(AssetA);
asset.add_dependency("B.b".into());
load_context.set_default_asset(asset);Ok(())})}fnextensions(&self) -> &[&str]{&["a"]}}#[derive(Clone,Copy,Default)]structAssetBLoader;implAssetLoaderTraitforAssetBLoader{fnload<'a>(&'a self,_bytes:&'a [u8],load_context:&'a mutLoadContext) -> BoxedFuture<Result<(),Error>>{Box::pin(asyncmove{use std::time::Duration;let asset = LoadedAsset::new(AssetB);// Pretend that we are parsing a large file (a .png)
std::thread::sleep(Duration::from_secs(10));
load_context.set_default_asset(asset);Ok(())})}fnextensions(&self) -> &[&str]{&["b"]}}#[derive(AssetCollection)]structACol{#[asset(path = "A.a")]_asset:Handle<AssetA>,}fntest_enter(assets_a:Res<Assets<AssetA>>,assets_b:Res<Assets<AssetB>>){assert!(assets_a.get::<AssetPath>("A.a".into()).is_some());println!("asset A loaded");assert!(assets_b.get::<AssetPath>("B.b".into()).is_some());println!("asset B loaded");}fnmain(){letmut app = App::new();
app
.add_plugins(DefaultPlugins).add_asset::<AssetA>().add_asset::<AssetB>().init_asset_loader::<AssetALoader>().init_asset_loader::<AssetBLoader>().add_loopless_state(GameState::Boot).add_enter_system(GameState::InGame, test_enter);AssetLoader::new(GameState::Boot).continue_to_state(GameState::InGame).with_collection::<ACol>().build(&mut app);
app.run();}
The text was updated successfully, but these errors were encountered:
InnocentusLime
changed the title
The lib doesn't wait for asset dependencies
bevy_asset_loader doesn't wait for asset dependencies
Jul 12, 2022
I don't know if it is weird or not. Looking through AssetServer code, it is revealed, that bevy handles dependencies asynchronously, but never .awaits them.
In this minimal example the app will crash, since neither bevy's
AssetServer
orbevy_asset_loader
wait for asset dependencies. SoAssetA
gets loaded successfully, but its slow dependency may not be loaded when you enterInGame
state.The text was updated successfully, but these errors were encountered: