Skip to content

[Adopted] Add a method for asynchronously waiting for an asset to load#15913

Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom
bushrat011899:Adopted14431
Oct 15, 2024
Merged

[Adopted] Add a method for asynchronously waiting for an asset to load#15913
alice-i-cecile merged 3 commits intobevyengine:mainfrom
bushrat011899:Adopted14431

Conversation

@bushrat011899
Copy link
Contributor

Objective

Currently, is is very painful to wait for an asset to load from the context of an async task. While bevy's AssetServer is asynchronous at its core, the public API is mainly focused on being used from synchronous contexts such as bevy systems. Currently, the best way of waiting for an asset handle to finish loading is to have a system that runs every frame, and either listens for AssetEvents or manually polls the asset server. While this is an acceptable interface for bevy systems, it is extremely awkward to do this in a way that integrates well with the async task system. At my work we had to create our own (inefficient) abstraction that encapsulated the boilerplate of checking an asset's load status and waking up a task when it's done.

Solution

Add the method AssetServer::wait_for_asset, which returns a future that suspends until the asset associated with a given Handle either finishes loading or fails to load.

Testing

  • CI

Notes

This is an adoption of #14431, the above description is directly from that original PR.

Co-Authored-By: Joseph <21144246+JoJoJet@users.noreply.github.com>
Comment on lines -436 to +439
infos.pending_tasks.insert(handle.id(), task);
{
let mut infos = infos;
infos.pending_tasks.insert(handle.id(), task);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is unrelated to the PR. I added this to suppress a Clippy warning around unused mut.

@bushrat011899 bushrat011899 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 X-Uncontroversial This work is generally agreed upon S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Async Deals with asynchronous abstractions labels Oct 14, 2024
@bushrat011899 bushrat011899 added this to the 0.15 milestone Oct 14, 2024
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 14, 2024
Flattened `pole_fn` code to reduce width, and explicitly convert `AssetId<A>` to an `UntypedAssetId` in `wait_for_asset` to avoid code-bloat.

Co-Authored-By: andriyDev <andriydzikh@gmail.com>
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 15, 2024
Merged via the queue into bevyengine:main with commit c1bb4b2 Oct 15, 2024
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 D-Async Deals with asynchronous abstractions S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants