Skip to content

Assets V2 - Error whilst using load_context.load_direct() to load a LoadedFolder asset #9932

@66OJ66

Description

@66OJ66

Bevy version

0.12-dev

What you did

I'm trying to implement a many -> 1 AssetLoader for item assets in my game.
e.g. there could be 100s of item assets, and I just want to combine them into one ItemMap asset.

Rather than manually maintaining a meta asset which just lists out the paths to each of these item assets, I'm trying to use a LoadedFolder within the AssetLoader to retrieve every item asset within a directory.

e.g.

fn load<'a>(
        &'a self,
        _reader: &'a mut Reader,
        _settings: &'a Self::Settings,
        load_context: &'a mut LoadContext,
    ) -> BoxedFuture<'a, Result<ItemMap, anyhow::Error>> {
        Box::pin(async move {
            // Load all item assets within the /assets/items directory (as a LoadedFolder asset)
            let loaded_folder = load_context.load_direct("items").await?;
            let folder = loaded_folder.get::<LoadedFolder>().unwrap();

            let mut items: HashMap<u64, Item> = HashMap::new();

            // Populate the ItemMap (and check for duplicate IDs)
            for handle in &folder.handles {
                let loaded_item = load_context.load_direct(handle.path().unwrap()).await?;
                let item = loaded_item.get::<Item>().unwrap();

                if let Some(existing_item) = items.insert(item.id, item.clone()) {
                    return Err(anyhow::Error::msg(format!(
                        "Duplicate ID detected: {}",
                        existing_item.id
                    )));
                }
            }

            Ok(ItemMap(items))
        })
    }

I've uploaded an example repo here: https://github.com/66OJ66/preprocessing_loaded_folder_testing

What went wrong

In short, this approach yields an error:

ERROR bevy_asset::server: Asset 'all.items.ron' encountered an error in preprocessing_loaded_folder_testing::assets::ItemMapAssetLoader: Failed to load dependency items path not found: items

even though the items directory exists.

Possibly load_context.load_direct isn't designed to handle LoadedFolder assets?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AssetsLoad files from disk to use for things like images, models, and soundsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions