NiklasEi / bevy_asset_loader

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

Race condition on multiple similar AssetLoaders #60

Closed Kaendor closed 2 years ago

Kaendor commented 2 years ago

Hello,

I am currently trying to apply clean asset loading to two of my Plugin.

As per the guide, I am initializing a loopless_state with app.add_loopless_state(GameState::Loading) before using any of my plugins.

I am doing the following :

In MeteorPlugin :

AssetLoader::new(GameState::Loading)
            .continue_to_state(GameState::Level)
            .with_collection::<MeteorAssets>()
            .build(app);

app.add_enter_system(GameState::Level, spawn_asteroid_field);

In ShipPlugin :

 AssetLoader::new(GameState::Loading)
            .continue_to_state(GameState::Level)
            .with_collection::<LoadingShipAssets>()
            .build(app);

app.add_enter_system(GameState::Level, setup_spaceship_system)

When I was using the AssetLoader in only one Plugin everything went fine. But as soon as I added asset collection loading to the other, I believe the GameState::Level was entered too soon and made my code panic since all the assets were not loaded yet.

It kinda looks like a race condition between each AssetLoader.

I thought when declaring more than one AssetLoader on a given state, it would aggregate all the collections needed, wait for all of them to load, and then transition to the given state.

Thank you for your time!

NiklasEi commented 2 years ago

Thank you for raising the issue. This should work, but I can reproduce, that it currently does not.

I'll fix it

Kaendor commented 2 years ago

Thanks a lot!