Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to directly load assets from World #12023

Merged

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented Feb 21, 2024

Objective

FromWorld is often used to group loading and creation of assets for resources.

With this setup, users often end up repetitively calling .resource::<AssetServer> and .resource_mut::<Assets<T>>, and may have difficulties handling lifetimes of the returned references.

Solution

Add extension methods to World to add and load assets, through a new extension trait defined in bevy_asset.

Other considerations

  • This might be a bit too "magic", as it makes implicit the resource access.
  • We could also implement DirectAssetAccessExt on App, but it didn't feel necessary, as FromWorld is the principal use-case here.

Changelog

  • Add the DirectAssetAccessExt trait, which adds the add_asset, load_asset and load_asset_with_settings method to the World type.

@nicopap nicopap 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 Feb 21, 2024
@valaphee
Copy link
Contributor

There is also the downside that it generates more code/or is slightly slower as it has to do a lookup each time you add an asset.

But it should be a "cold" anyway.

Copy link
Contributor

@afonsolage afonsolage left a comment

Choose a reason for hiding this comment

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

The examples looks a lot cleaner and more intuitive, I like it

@nicopap nicopap force-pushed the add-direct-asset-server-access branch from 97da72d to 6fa0a59 Compare February 25, 2024 16:30
@nicopap nicopap 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 Feb 26, 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.

I like the diff in the examples a lot, and I'm pleased with the code quality here.

It is more magical, but this method will also be more robust to any future changes in how asset loading works.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 27, 2024
Merged via the queue into bevyengine:main with commit f7f7e32 Feb 27, 2024
25 checks passed
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.

5 participants