Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
66OJ66 opened this issue Sep 26, 2023 · 2 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@66OJ66
Copy link
Contributor

66OJ66 commented Sep 26, 2023

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:/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?

@66OJ66 66OJ66 added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Sep 26, 2023
@cart cart added A-Assets Load files from disk to use for things like images, models, and sounds and removed S-Needs-Triage This issue needs to be labelled labels Sep 27, 2023
@cart
Copy link
Member

cart commented Sep 27, 2023

Yup load_direct is the "direct" equivalent to load, which does not support folders. In apps, there is a separate load_folder api for that.

However in loaders and processors, we do not currently have a load_folder_direct api. This is largely because it introduces additional complexity when it comes to determining when a dependency changes.

We need to be able to re-run processors whenever the "processor dependencies" change (aka "x_direct" calls). Currently, with load_direct(some_path), we can just add some_path to the processor dependencies for the asset and then re-process whenever some_path changes.

For some load_folder_direct api, we can't just append a list of the files currently in the folder to the dependency list, as that isn't what the actual "requested input" is. We need a new concept of "folder dependencies" that allows us to reprocess an asset (using load_folder_direct(some_folder)) whenever any asset in the some_folder is added, removed, or changed. This is possible, but it will require some careful engineering.

@66OJ66
Copy link
Contributor Author

66OJ66 commented Oct 7, 2023

That makes sense - thank you!
I'll automate the creation of a dependency list meta-asset as a workaround for now.

Might be worth adding this under the 'Things to Investigate' section in #9714?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants
@cart @66OJ66 and others