NiklasEi / bevy_asset_loader

Bevy plugin helping with asset loading and organization
Apache License 2.0
489 stars 56 forks source link

Using a loaded folder #32

Closed BlitzBanana closed 1 year ago

BlitzBanana commented 2 years ago

Hello,

I see that you are currently implementing a feature to load a folder into a Vec<Handle<..>>. I'm farily new to Bevy and Rust, so maybe I'm missing something here, but how do you plan this to be usable ? Since Handles are just u64 identifiers, how do I know which handle I want to use/retreive ?

Here is an example:

#[derive(AssetCollection)]
struct IconCollection {
    #[asset(path = "icons", folder(typed))]
    all: Vec<Handle<Image>>,
}

fn show_actionbar_ui(
    ui: ResMut<..>,
    icon_collection: Res<IconCollection>,
    selected_actionbar_slot: Res<SelectedActionbarSlot>,
    selected_warrior: Res<SelectedWarrior>,
    wariors: Query<&Skills, With<Warrior>>
) {
    if let Some(skills) = warriors.get(selected_warrior.0) {
        for skill in skills.iter() {
            let icon_handle = icon_collection.all. // ???? = skill.icon_label;
            ui.show_icon(icon_handle);
        }
    }
}

Could it be more practical to load folders into a HashMap<String, Handle<..>>, using the filename as a key ?

Anyway, thanks for your work, this lib is great.

NiklasEi commented 2 years ago

The current implementation is only intended for use cases where you do not care which handle you are getting. Examples are random variants of things like tree or rock textures, or variants of sound effects.

In your case, it looks like you want the different skills to be separate handles on the asset collection.

I like the idea of a map from file names to handles. Thank you for the suggestion.

NiklasEi commented 2 years ago

~Looking at the current asset server it does not seem to be possible to get the file path from a handle. If this would ever get possible we could load a directory as a map file path <-> handle, but not at the moment.~

Edit: this is just plain wrong. I overlooked the get_handle_path method on the AssetServer.

For now, I'll close this.

Chaanks commented 1 year ago

Hello, I am interested too, is it still impossible? @NiklasEi

NiklasEi commented 1 year ago

I think I overlooked a rather simple way of doing this. Thanks for the ping :slightly_smiling_face:

NiklasEi commented 1 year ago

What do you think about the following: https://github.com/NiklasEi/bevy_asset_loader/blob/aa5fc0f3e94870e3a5c268616b4f3c7a916df9b2/bevy_asset_loader/examples/load_folder_as_map.rs#L20-L38

Using the new option now, I am not that convinced anymore that this is a valuable addition. The only difference from asset_server.get_handle("folder/image.png") is that I stripped away the folder path (so collection.get("image.png").unwrap()) and that the collection knows if the asset actually exists.

Chaanks commented 1 year ago

I'm a new user of this crate and Bevy in general, so not a good opinion on this. I would say that even if asset_server.get_handle() is similar, this addition will reduce the boilerplate needed to load a collection from a folder in a way that we can access it using filename as key.