Skip to content

Commit

Permalink
Merge branch 'main' of github.com:NiklasEi/bevy_asset_loader into app…
Browse files Browse the repository at this point in the history
…_configure_loading_state
  • Loading branch information
NiklasEi committed Nov 23, 2023
2 parents ff6c230 + 02b711f commit 9bb0391
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum GameState {
}
```

The [full_collection](bevy_asset_loader/examples/full_collection.rs) example showcases all the different kinds of fields that an asset collection can contain using only derive macro attributes.
The [full_collection](/bevy_asset_loader/examples/full_collection.rs) example showcases all the different kinds of fields that an asset collection can contain using only derive macro attributes.

### Dynamic assets

Expand All @@ -93,7 +93,9 @@ struct ImageAssets {
}
```

The keys `player` and `tree` in the example above should either be set manually in the `DynamicAssets` resource prior to the loading state (see the [manual_dynamic_asset](bevy_asset_loader/examples/manual_dynamic_asset.rs) example), or be part of a dynamic assets file (see [dynamic_asset](bevy_asset_loader/examples/dynamic_asset.rs)). A dynamic assets file for the collection above might look like this:
The keys `player` and `tree` in the example above should either be set manually in the `DynamicAssets` resource prior to the loading state
(see the [manual_dynamic_asset](/bevy_asset_loader/examples/manual_dynamic_asset.rs) example), or be part of a dynamic assets file (see [dynamic_asset](/bevy_asset_loader/examples/dynamic_asset.rs)).
A dynamic assets file for the collection above might look like this:
```ron
({
"player": File (
Expand All @@ -111,11 +113,11 @@ The file ending is `.assets.ron` by default, but can be configured via `LoadingS

Dynamic assets can be optional. This requires the derive attribute `optional` on the field and the type to be an `Option`. The value of the field will be `None` in case the given key cannot be resolved at run time.

The example [full_dynamic_collection](bevy_asset_loader/examples/full_dynamic_collection.rs) shows all supported field types for dynamic assets. Note that adding a dynamic asset file to a loading state requires the `AssetServer` resource to be available. In most cases that means that you should add the `DefaultPlugins` before configuring your loading state.
The example [full_dynamic_collection](/bevy_asset_loader/examples/full_dynamic_collection.rs) shows all supported field types for dynamic assets. Note that adding a dynamic asset file to a loading state requires the `AssetServer` resource to be available. In most cases that means that you should add the `DefaultPlugins` before configuring your loading state.

### Custom dynamic assets

You can define your own types to load as dynamic assets. Take a look at the [custom_dynamic_assets.rs](bevy_asset_loader/examples/custom_dynamic_assets.rs) example for some code.
You can define your own types to load as dynamic assets. Take a look at the [custom_dynamic_assets.rs](/bevy_asset_loader/examples/custom_dynamic_assets.rs) example for some code.

## Supported asset fields

Expand Down Expand Up @@ -311,7 +313,7 @@ The corresponding dynamic asset would be

### Standard materials

You can directly load standard materials if you enable the feature `3d`. For a complete example please take a look at [standard_material.rs](bevy_asset_loader/examples/standard_material.rs).
You can directly load standard materials if you enable the feature `3d`. For a complete example please take a look at [standard_material.rs](/bevy_asset_loader/examples/standard_material.rs).

```rust
use bevy::prelude::*;
Expand Down Expand Up @@ -343,7 +345,7 @@ struct MyAssets {

### Texture atlases

You can directly load texture atlases from sprite sheets if you enable the feature `2d`. For a complete example please take a look at [atlas_from_grid.rs](bevy_asset_loader/examples/atlas_from_grid.rs).
You can directly load texture atlases from sprite sheets if you enable the feature `2d`. For a complete example please take a look at [atlas_from_grid.rs](/bevy_asset_loader/examples/atlas_from_grid.rs).

```rust
use bevy::prelude::*;
Expand Down Expand Up @@ -389,23 +391,23 @@ Any field in an asset collection without any attribute is required to implement

## Initializing FromWorld resources

In situations where you would like to prepare other resources based on your loaded asset collections you can use `App::init_resource_after_loading_state` to initialize `FromWorld` resources. See [init_resource.rs](bevy_asset_loader/examples/init_resource.rs) for an example that loads two images and then combines their pixel data into a third image.
In situations where you would like to prepare other resources based on your loaded asset collections you can use `App::init_resource_after_loading_state` to initialize `FromWorld` resources. See [init_resource.rs](/bevy_asset_loader/examples/init_resource.rs) for an example that loads two images and then combines their pixel data into a third image.

`App::init_resource_after_loading_state` does the same as Bevy's `App::init_resource`, but at a different point in time. While Bevy inserts your resources at the very beginning, `bevy_asset_loader` will initialize them only after your loaded asset collections are inserted. That means you can use your asset collections in the `FromWorld` implementation.

## Progress tracking

With the feature `progress_tracking`, you can integrate with [`iyes_progress`][iyes_progress] to track asset loading during a loading state. This, for example, enables progress bars.

See [`progress_tracking`](bevy_asset_loader/examples/progress_tracking.rs) for a complete example.
See [`progress_tracking`](/bevy_asset_loader/examples/progress_tracking.rs) for a complete example.

### A note on system ordering

The loading state is organized in a private schedule that runs in a single system during the `Update` schedule. If you want to explicitly order against the system running the loading state, you can do so with the system set `LoadingStateSet`.

## Failure state

You can configure a failure state in case some asset in a collection fails to load by calling `on_failure_continue_to` with a state (see [`failure_state`](bevy_asset_loader/examples/failure_state.rs) example). If no failure state is configured and some asset fails to load, your application will be stuck in the loading state.
You can configure a failure state in case some asset in a collection fails to load by calling `on_failure_continue_to` with a state (see [`failure_state`](/bevy_asset_loader/examples/failure_state.rs) example). If no failure state is configured and some asset fails to load, your application will be stuck in the loading state.

In most cases this happens, an asset file is missing or a certain file ending does not have a corresponding asset loader. In both of these cases the application log should help since Bevy prints warnings about those issues.

Expand All @@ -415,7 +417,7 @@ Although the pattern of a loading state is quite nice (imo), you might have reas

Asset collections loaded without a loading state do not support folders or dynamic assets, since these cannot instantly create handles that will eventually point to the loaded assets.

You can directly initialise asset collections on the bevy `App` or `World`. See [no_loading_state.rs](bevy_asset_loader/examples/no_loading_state.rs) for a complete example.
You can directly initialise asset collections on the bevy `App` or `World`. See [no_loading_state.rs](/bevy_asset_loader/examples/no_loading_state.rs) for a complete example.

```rust no_run
use bevy::prelude::*;
Expand Down Expand Up @@ -467,7 +469,7 @@ Dual-licensed under either of

at your option.

Assets in the examples might be distributed under different terms. See the [readme](bevy_asset_loader/examples/README.md#credits) in the `bevy_asset_loader/examples` directory.
Assets in the examples might be distributed under different terms. See the [readme](/bevy_asset_loader/examples/README.md#credits) in the `bevy_asset_loader/examples` directory.

## Contribution

Expand Down
6 changes: 3 additions & 3 deletions bevy_asset_loader/src/asset_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub trait AssetCollection: Resource {
fn load(world: &mut World) -> Vec<UntypedHandle>;
}

/// Extension trait for [`App`](::bevy::app::App) enabling initialisation of [asset collections](crate::asset_collection::AssetCollection)
/// Extension trait for [`App`] enabling initialisation of [asset collections](crate::asset_collection::AssetCollection)
pub trait AssetCollectionApp {
/// Initialise an [`AssetCollection`](crate::asset_collection::AssetCollection)
/// Initialise an [`AssetCollection`]
///
/// This function does not give any guaranties about the loading status of the asset handles.
/// If you want to use a loading state, you do not need this function! Instead use an [`LoadingState`](crate::loading_state::LoadingState)
Expand All @@ -56,7 +56,7 @@ impl AssetCollectionApp for App {
}
}

/// Extension trait for [`World`](::bevy::ecs::world::World) enabling initialisation of [asset collections](AssetCollection)
/// Extension trait for [`World`] enabling initialisation of [asset collections](AssetCollection)
pub trait AssetCollectionWorld {
/// Initialise an [`AssetCollection`]
///
Expand Down
4 changes: 2 additions & 2 deletions bevy_asset_loader/src/dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub trait DynamicAsset: Debug + Send + Sync {

/// Resource to dynamically resolve keys to assets.
///
/// This resource is set by a [`LoadingState`](crate::loading_state::LoadingState) and is read when entering the corresponding Bevy [`State`](State).
/// If you want to manage your dynamic assets manually, they should be configured in a previous [`State`](State).
/// This resource is set by a [`LoadingState`](crate::loading_state::LoadingState) and is read when entering the corresponding Bevy [`State`](::bevy::ecs::schedule::State).
/// If you want to manage your dynamic assets manually, they should be configured in a previous [`State`](::bevy::ecs::schedule::State).
///
/// See the `manual_dynamic_asset` example.
#[derive(Resource, Default)]
Expand Down
6 changes: 3 additions & 3 deletions bevy_asset_loader/src/loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
}
}

/// The [`LoadingState`] will set this Bevy [`State`](State) after all asset collections
/// The [`LoadingState`] will set this Bevy [`State`] after all asset collections
/// are loaded and inserted as resources.
/// ```edition2021
/// # use bevy_asset_loader::prelude::*;
Expand Down Expand Up @@ -205,7 +205,7 @@ where
self
}

/// The [`LoadingState`] will set this Bevy [`State`](State) if an asset fails to load.
/// The [`LoadingState`] will set this Bevy [`State`] if an asset fails to load.
/// ```edition2021
/// # use bevy_asset_loader::prelude::*;
/// # use bevy::prelude::*;
Expand Down Expand Up @@ -444,7 +444,7 @@ where
}
}

/// Systems in this set check the loading state of assets and will change the [`InternalLoadingState`] accordingly.
/// Systems in this set check the loading state of assets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
pub struct LoadingStateSet<S: States>(pub S);

Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader/src/standard_dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<K: Into<String> + Sync + Send + 'static> Command for RegisterStandardDynami
/// The asset defining a mapping from asset keys to dynamic assets
///
/// These assets are loaded at the beginning of a loading state
/// and combined in [`DynamicAssets`](DynamicAssets).
/// and combined in [`DynamicAssets`].
#[derive(serde::Deserialize, Asset, TypePath)]
pub struct StandardDynamicAssetCollection(pub HashMap<String, StandardDynamicAsset>);

Expand Down

0 comments on commit 9bb0391

Please sign in to comment.