[Adopted] Add a method for asynchronously waiting for an asset to load#15913
Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom Oct 15, 2024
Merged
[Adopted] Add a method for asynchronously waiting for an asset to load#15913alice-i-cecile merged 3 commits intobevyengine:mainfrom
alice-i-cecile merged 3 commits intobevyengine:mainfrom
Conversation
Co-Authored-By: Joseph <21144246+JoJoJet@users.noreply.github.com>
bushrat011899
commented
Oct 14, 2024
Comment on lines
-436
to
+439
| infos.pending_tasks.insert(handle.id(), task); | ||
| { | ||
| let mut infos = infos; | ||
| infos.pending_tasks.insert(handle.id(), task); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This is unrelated to the PR. I added this to suppress a Clippy warning around unused mut.
andriyDev
approved these changes
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>
MiniaczQ
approved these changes
Oct 15, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Currently, is is very painful to wait for an asset to load from the context of an
asynctask. While bevy'sAssetServeris 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 forAssetEventsor 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 theasynctask 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 givenHandleeither finishes loading or fails to load.Testing
Notes
This is an adoption of #14431, the above description is directly from that original PR.