Skip to content

Mutable assets #31

Description

@palapapa

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>.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions