Skip to content

Allow loading assets with custom async behavior#13700

Merged
alice-i-cecile merged 5 commits intobevyengine:mainfrom
joseph-gio:add-asset-async
Jun 6, 2024
Merged

Allow loading assets with custom async behavior#13700
alice-i-cecile merged 5 commits intobevyengine:mainfrom
joseph-gio:add-asset-async

Conversation

@joseph-gio
Copy link
Member

Objective

Currently, bevy supports custom asset loading via AssetServer:;add, which allows you to add arbitrary assets to the asset system and returns a handle to it. However this only works for assets that have already been fully loaded. If your loading logic involves any async, you need to wait until the asset is done loading before adding it to the server. This is problematic, as the Handle does not get allocated until the very end, which makes it very difficult to use and defeats the value of having handles for asynchronously-loaded assets.

Solution

Add the method AssetServer::add_async. This has the same behavior as AssetServer::add, only it accepts a future instead of a fully loaded asset.

Testing

I added an identical method to my company's fork of bevy, which works in our app. I'm not quite sure how to go about adding an actual unit test for asset loading behvior, but I will note that AssetServer::add also does not appear to have any tests.


Changelog

  • Added AssetServer::add_async, which allows adding assets with custom asynchronous loading behavior to the AssetServer

@joseph-gio joseph-gio added A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jun 5, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable enough. I can see the use of this, and this seems reasonably constructed.

Agreed on the testing front: we need to sit down and figure out a testing strategy for bevy_assets more cohesively.

@joseph-gio
Copy link
Member Author

The Eq bound on AssetLoadError seems a bit problematic here. I'm not sure why it's been added but for now I'm just going to remove the new enum variant I added

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea! There's just enough complexity here (IoTaskPool, the event_sender, etc.) that this encapsulation really improves the user experience, but is still overall pretty simple. I concur that a better testing story is needed for bevy_asset, but I don't think that is necessary to get this merged.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jun 6, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 6, 2024
Merged via the queue into bevyengine:main with commit 9389de5 Jun 6, 2024
@joseph-gio joseph-gio deleted the add-asset-async branch June 6, 2024 03:41
github-merge-queue bot pushed a commit that referenced this pull request Jun 10, 2024
# Objective

The method `AssetServer::add_async` (added in
#13700) requires a future that
returns an `AssetLoadError` error, which was a bit of an oversight on my
part, as that type of error only really makes sense in the context of
bevy's own asset loader -- returning it from user-defined futures isn't
very useful.

## Solution

Allow passing custom error types to `add_async`, which get cast into a
trait object matching the form of `AssetLoader::load`. If merged before
the next release this will not be a breaking change
mockersf pushed a commit that referenced this pull request Jun 10, 2024
# Objective

The method `AssetServer::add_async` (added in
#13700) requires a future that
returns an `AssetLoadError` error, which was a bit of an oversight on my
part, as that type of error only really makes sense in the context of
bevy's own asset loader -- returning it from user-defined futures isn't
very useful.

## Solution

Allow passing custom error types to `add_async`, which get cast into a
trait object matching the form of `AssetLoader::load`. If merged before
the next release this will not be a breaking change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants