NiklasEi / bevy_asset_loader

Bevy plugin helping with asset loading and organization
Apache License 2.0
459 stars 52 forks source link

Not changing out of loading state when using multiple dynamic asset collections #99

Closed spicy-moves closed 1 year ago

spicy-moves commented 1 year ago

I'm using multiple dynamic asset collections to split loading up over plugins. This works with multiple non-dynamic collections, and with a single dynamic collection, but with multiple dynamic collections it gets stuck on the loading state. Is this a bug or am I missing some prerequisite for dynamic collections?

Example code:

main.rs

use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use iyes_loopless::prelude::*;

struct HatGuyPlugin;

impl Plugin for HatGuyPlugin {
    fn build(&self, app: &mut App) {
        app.add_loading_state(
            LoadingState::new(GameState::Load)
                .continue_to_state(GameState::Play)
                .on_failure_continue_to_state(GameState::Fail)
                .with_dynamic_collections::<StandardDynamicAssetCollection>(vec!["hat-guy.assets"])
                .with_collection::<OverlayAssets>(),
        );
    }
}

#[derive(AssetCollection, Default, Debug, Resource)]
pub struct OverlayAssets {
    #[asset(key = "hat-guy")]
    pub hat_guy: Handle<Image>,
}

struct SenseiPlugin;

impl Plugin for SenseiPlugin {
    fn build(&self, app: &mut App) {
        app.add_loading_state(
            LoadingState::new(GameState::Load)
                .continue_to_state(GameState::Play)
                .on_failure_continue_to_state(GameState::Fail)
                .with_dynamic_collections::<StandardDynamicAssetCollection>(vec!["sensei.assets"])
                .with_collection::<SenseiAssets>(),
        );
    }
}

#[derive(AssetCollection, Default, Debug, Resource)]
pub struct SenseiAssets {
    #[asset(key = "sensei")]
    pub sensei: Handle<Image>,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_loopless_state(GameState::Load)
        .add_plugin(HatGuyPlugin)
        .add_plugin(SenseiPlugin)
        .add_system(debug)
        .run();
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
enum GameState {
    Load,
    Play,
    Fail,
}

fn debug(state: Res<CurrentState<GameState>>) {
    if state.is_changed() {
        println!("State changed to {:?}.", state.0);
    }
}

sensei.assets

({
    "sensei": File (
    path: "sensei.png",
    ),
})

hat-guy.assets

({
    "hat_guy": File (
    path: "hat-guy.png",
    ),
})
NiklasEi commented 1 year ago

It's something between a bug and a missing feature. What you can change for it to work now:

  1. only have one with_dynamic_collections call listing both files
  2. use the same key for hat guy (hat_guy in the file vs hat-guy in the code)

You can keep the issue open until I added support for multiple calls to with_dynamic_collections.

NiklasEi commented 1 year ago

If you use the main branch of bevy_asset_loader, point 1 from my comment above should no longer be necessary.