Would it be possible to add a feature that lets you mutate an asset? Bevy's asset system seems to support both mutating assets and hot-reloading. The documentation of Handle says that hot-reloading is implemented using RwLock, so would it be possible to implement mutable assets by exposing this lock to the user?
If someone needs mutable assets, here is a possible workaround (I haven't actually implemented this yet, so this may not be the exact pattern):
struct MyAsset {
// Anything you want to put in this asset
}
impl Asset for MyAsset {
fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError> {
/* Any logic to load your asset. If you need to read a file to
construct `MyAssetInner`, read it using `cache.source().read()` so
that the `AssetCache` registers that file as the dependency of this
asset and calls this method automatically when that file changes. */
}
}
struct MutableAsset<T>(RwLock<T>);
impl<A: Asset> Asset for MutableAsset<A> {
const HOT_RELOADED: bool = A::HOT_RELOADED;
fn load(cache: &AssetCache, id: &SharedString) -> Result<Self, BoxedError> {
Ok(Self(RwLock::new(A::load(cache, id)?)))
}
}
You can then use AssetCache::load to load MutableAsset<MyAsset>.
Would it be possible to add a feature that lets you mutate an asset? Bevy's asset system seems to support both mutating assets and hot-reloading. The documentation of
Handlesays that hot-reloading is implemented usingRwLock, so would it be possible to implement mutable assets by exposing this lock to the user?If someone needs mutable assets, here is a possible workaround (I haven't actually implemented this yet, so this may not be the exact pattern):
You can then use
AssetCache::loadto loadMutableAsset<MyAsset>.