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

Assets with embedded handles to other assets #9644

Closed
viridia opened this issue Aug 30, 2023 · 2 comments
Closed

Assets with embedded handles to other assets #9644

viridia opened this issue Aug 30, 2023 · 2 comments
Labels
C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled

Comments

@viridia
Copy link
Contributor

viridia commented Aug 30, 2023

What problem does this solve or what need does it fill?

Complex assets such as scenes, UI layouts, and other high-level game data (quests, dialogues, skills, etc.) often contain embedded references to other, lower-level assets. I want to simplify how these dependent assets are loaded by allowing embedded asset references to be resolved by the asset loader for the containing asset.

For example, I'm working on a GUI asset format which contains a tree of nodes (widgets) that can reference other assets (stylesheets). A typical widget looks like this (in JSON):

{
  "type": "element",
  "id": "left",
  "styleset": ["#/styles/sub-panel"],
  "style": {
    "width": 200
  }
},

The styleset property is a list of relative asset paths (resolving using my other PR #9473). When I decode the asset, the asset path is deserialized into a string; later, there's a system which walks the tree and calls server.load() on each asset path.

However, it would be more efficient if I could create the asset handles in the decoder itself. This has several advantages:

  • It means less complexity, since I no longer have to have a separate system for loading the dependent assets.
  • It means faster loading, since the dependent handles get added earlier in the lifecycle.
  • It means that I can declare the linked assets as dependencies using the add_asset_dependency method.

Unfortunately, an asset loader doesn't have access to the APIs needed to load another asset (at least, not that I can see). The only context available to an asset loader is LoadContext. LoadContext does have a get_handle method, but it takes a handle id, not a path. There doesn't seem to be any equivalent to server.load().

A description of why this particular feature should be added.

What solution would you like?

The easiest solution would be add a method to LoadContext that returns a handle given an AssetPath.

Additional Context

Note that the embedded references are handles, not the loaded data. In other words, the widget contains a handle to a stylesheet, not a stylesheet itself. This is by design - it facilitates hot reloading of stylesheets, so that when I edit the JSON file it doesn't need to completely re-create the UI, only the styles.

@viridia viridia added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Aug 30, 2023
@mockersf
Copy link
Member

get_handle takes a Into<HandleId>, which AssetPath implements

@viridia
Copy link
Contributor Author

viridia commented Aug 30, 2023

OK, that's interesting. I would not have been able to figure that out just from reading the documentation.

Also, I'm not sure it completely addresses the issue - get_handle() creates a strong handle, but it doesn't appear to actually initiate loading of the asset for that path. Here's what the implementation looks like:

pub fn get_handle<I: Into<HandleId>, T: Asset>(&self, id: I) -> Handle<T> {
    Handle::strong(id.into(), self.ref_change_channel.sender.clone())
}

@viridia viridia closed this as completed Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled
Projects
None yet
Development

No branches or pull requests

2 participants