NiklasEi / bevy_asset_loader

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

AssetServer does not exist in the `World` when trying to use dynamic asset #128

Closed zwazel closed 1 year ago

zwazel commented 1 year ago

Hello! I'm trying to use the DynamicAsset feature. Until now I've been only using the not dynamic asset loading features, and everything worked fine. For now I'm just trying to load a png as a StandardMaterial, as shown in one of your examples.

So I changed the cargo.toml file to add the dynamic feature:

bevy_asset_loader = { version = "0.16.0", features = ["standard_dynamic_assets","3d", "2d"] 

and added the my_assets.assets.ron file inside the assets folder:

({
    "texture.floor": StandardMaterial (
        path: "textures/prototyping/green/texture_09.png",
    ),
})

I've defined my resource:

#[derive(AssetCollection, Resource, Default, Reflect)]
#[reflect(Resource)]
struct WorldTextures {
    #[asset(key = "texture.floor")]
    floor_texture: Handle<StandardMaterial>,
}

added it to the loading state

app.add_dynamic_collection_to_loading_state::<_, StandardDynamicAssetCollection>(
                LoadState::LoadingMapAndSetupResources,
                "my_assets.assets.ron",
            )
            .add_collection_to_loading_state::<_, WorldTextures>(
                LoadState::LoadingMapAndSetupResources,
            );

Did I miss anything? Because anytime I try to start the game i get a runtime error saying the AssetServer doesnt exist. I only get this error when enabling the dynamic asset feature. the error:

C:/Users/zwaze/.cargo/bin/cargo.exe run --color=always --package project-getting-control --bin main windowed
    Finished dev [optimized + debuginfo] target(s) in 0.29s
     Running `target\debug\main.exe windowed`
thread 'main' panicked at 'Requested resource bevy_asset::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.', C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_asset-0.10.1\src\assets.rs:329:43
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/af06dce64bf87ea9206bdf6cff61c144b9ce8458/library\std\src\panicking.rs:577
   1: core::panicking::panic_fmt
             at /rustc/af06dce64bf87ea9206bdf6cff61c144b9ce8458/library\core\src\panicking.rs:67
   2: bevy_ecs::world::World::resource
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.10.1\src\world\mod.rs:968
   3: bevy_asset::assets::impl$2::add_asset<bevy_asset_loader::standard_dynamic_asset::StandardDynamicAssetCollection>
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_asset-0.10.1\src\assets.rs:338
   4: bevy_common_assets::ron::impl$0::build<bevy_asset_loader::standard_dynamic_asset::StandardDynamicAssetCollection>
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_common_assets-0.6.0\src\ron.rs:17
   5: bevy_app::app::App::add_boxed_plugin
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.10.1\src\app.rs:767
   6: bevy_app::app::App::add_plugin<bevy_common_assets::ron::RonAssetPlugin<bevy_asset_loader::standard_dynamic_asset::StandardDynamicAssetCollection> >
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.10.1\src\app.rs:747
   7: bevy_asset_loader::loading_state::LoadingState<project_getting_control::LoadState>::build<project_getting_control::LoadState>
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_asset_loader-0.16.0\src\loading_state.rs:346
   8: bevy_asset_loader::loading_state::impl$5::add_loading_state<project_getting_control::LoadState>
             at C:\Users\zwaze\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_asset_loader-0.16.0\src\loading_state.rs:661
   9: main::main
             at .\src\bin\main.rs:67
  10: core::ops::function::FnOnce::call_once
             at /rustc/af06dce64bf87ea9206bdf6cff61c144b9ce8458\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\main.exe windowed` (exit code: 101)

Process finished with exit code 101
NiklasEi commented 1 year ago

Are you maybe adding the loading state before adding the default plugins?

zwazel commented 1 year ago

good call, you're right. Thanks!

NiklasEi commented 1 year ago

Let's leave it open until I improved the readme. It still includes an example where the default plugins are added afterwards.

NiklasEi commented 1 year ago

The example code was fine because it did not add dynamic assets. But I added a note about the ordering in the readme and generally moved the default plugins to the front in all example code.