NiklasEi / bevy_asset_loader

Bevy plugin helping with asset loading and organization
Apache License 2.0
452 stars 53 forks source link

Possible to call `add_dynamic_collection_to_loading_state` and `add_collection_to_loading_state` in plugin? #163

Closed poperigby closed 7 months ago

poperigby commented 8 months ago

Is it possible to do this? I tried doing so like this:

impl Plugin for PlayerPlugin {
    fn build(&self, app: &mut App) {
        app.add_dynamic_collection_to_loading_state::<AppState, StandardDynamicAssetCollection>(
            AppState::Loading,
            "player/player.assets.ron",
        )
        .add_collection_to_loading_state::<AppState, PlayerAssets>(AppState::Loading)
    }
}

But it just crashed every time with

called `Option::unwrap()` on a `None` value

I'd just like to be able to keep everything organized.

NiklasEi commented 8 months ago

You can do this, but the loading state needs to be added to your app before this plugin is added.

NiklasEi commented 8 months ago

The error should be better, I'll look into that.

poperigby commented 8 months ago

You can do this, but the loading state needs to be added to your app before this plugin is added.

Thank you. I tried doing this in my main.rs:

fn main() {
    App::new()
        .add_state::<AppState>()
        .add_loading_state(LoadingState::new(AppState::Loading).continue_to_state(AppState::InGame))
        .add_plugins((
            DefaultPlugins.set(ImagePlugin::default_nearest()),
            RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
            RapierDebugRenderPlugin::default(),
            // Game plugins
            MainMenuPlugin,
            PlayerPlugin,
        ))
        .add_systems(Startup, setup)
        .run();
}

Then I have this in my player.rs:

pub struct PlayerPlugin;

impl Plugin for PlayerPlugin {
    fn build(&self, app: &mut App) {
        app.add_dynamic_collection_to_loading_state::<AppState, StandardDynamicAssetCollection>(
            AppState::Loading,
            "player/player.assets.ron",
        )
        .add_collection_to_loading_state::<AppState, PlayerAssets>(AppState::Loading)
        .add_systems(OnEnter(AppState::InGame), setup)
        .add_systems(Update, input.run_if(in_state(AppState::InGame)));
    }
}

It's now giving me this error on running:

thread 'main' panicked at /home/cassidy/.local/share/cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_asset-0.12.0/src/lib.rs:296:20:
Requested resource bevy_asset::server::AssetServer does not exist in the `World`.
                Did you forget to add it using `app.insert_resource` / `app.init_resource`?
                Resources are also implicitly added via `app.add_event`,
                and can be added by plugins.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
NiklasEi commented 8 months ago

Do you have the bevy feature bevy_asset enabled?

poperigby commented 8 months ago

I'm pretty sure it's enabled by default, but I explicitly enabled it and it's still not working.

Here's the rest of my source code, if that's helpful: https://codeberg.org/PopeRigby/platformer

NiklasEi commented 7 months ago

The order of adding plugins, the loading state and the assets is important here. You need to first add the default plugins (aka add Bevy to your app), then you can add the loading state and afterwards you can add plugins that add things to the loading state. The loading state needs the AssetServer which comes with the DefaultPlugins. With the order above I can add your collection to the loading state in the PlayerPlugin.

FYI: idle.png doesn't seem to be a valid png image. Neither GIMP nor Bevy can open it.

NiklasEi commented 7 months ago

I will improve the error messages that bevy_asset_loader panics with when the asset server is not reachable (missing assets feature or no default plugins). About the invalid png: I guess there is no error because of bevyengine/bevy#10515.

poperigby commented 7 months ago

There we go, perfect! Thank you so much :slightly_smiling_face:. That might be because I uploaded it with Git LFS.