Lab: Hot Reloading
This lab is about hot-reloading game assets, based on the discussion in Workflow and Tooling.
You'll want to make an Assets or AssetManager struct which hands out "asset handles" rather than raw assets like textures or whatever. There are two main ways code can be set up to do something when handles are invalidated:
- A callback is called; this is not great in Rust due to ownership issues. A different approach would be for each consumer of assets to have an MPSC channel (like a mailbox) in which they can receive messages when asset handles are invalidated.
- When accessing a resource from a handle, the calling code also provides some token (or checks whether the data behind the handle is invalid or different from the last time); then the new data can be put into bind groups or whatever.
In the special case of wgpu's Texture, Textures are writable from anywhere so there's no need to use a separate handle type. But for other types like audio files or game level data files, you want to be able to notice when they've changed and do something like restart the song or reload the level.
(2) is probably the smoother way to go; the asset manager could hand out handles that are only valid for one underlying version of the asset, and accessing a handle can return a Result. If the handle version matches the underlying asset version, this can be Ok(some_asset)
; after a reload has happened they can return Err(Reloaded(new_handle))
, in which case the calling code should replace its handle with the new one and do something appropriate like remake bind groups or unload and reload the level objects.