bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.23k stars 3.57k forks source link

Allow "named" assets and overriding "names" #1810

Open huhlig opened 3 years ago

huhlig commented 3 years ago

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

Currently only assets loaded by AssetServer::load have names and it's fixed as the name of the path. Looking up a loaded assets is as simple as calling load a second time. However assets that are created don't have this luxury thus requiring custom cataloging of handles to provide lookup.

What solution would you like?

Add new methods

AssetServer::get_handle_by_name(name: &str) -> Option<Handle<T>>;
Assets::get_handle_by_name(name: &str) -> Option<Handle<T>>;
Assets::add_as(name: &str, asset: T) -> Handle<T>;

What alternative(s) have you considered?

Adding custom catalog for assets. This while doable seems like an anti-pattern as all handles will need to be cloned into the catalog.

mockersf commented 3 years ago

The way it's down to get the same handle from a path is that the handle is derived from the path directly.

If we add handle-derived-from-name, what happens if I use the same name for different assets? New one rejected, or old ones now point to new asset?

Should a named asset be cleaned if there are no current strong handle to it, even though the user could still recreate an handle?

huhlig commented 3 years ago

Currently we already have handles by name, the name is just fixed from the path when it's loaded, and technically if we have multiple asset packs we would have the same issue.

mockersf commented 3 years ago

Path are specials in that they represent a link to the original file, so:

Also, for the method on asset server: AssetServer::get_handle_by_name(name: &str) -> Handle<T>;: if the user never adds the name through the Assets::add_as methods, it will returns an handle that will never resolve.

huhlig commented 3 years ago

You're right, it should return Option::None