Reload Assets at Runtime?

I know it’s possible to load assets at runtime. Is it possible to actually just reload all the assets from the assets directory?

I’m working on something like a book. I would like to be able to drop assets into the source assets directory, copy them over to the export assets directory for the right platform, and have my game detect and reload all assets if that happens.

Is that possible? Is there some way to force reloading all assets?

If you run lime update again while it is running, the Assets should dispatch a change event, and should dump all of its cache, so the next time you getBitmapData (and so on) it should be the new copies of the files :slight_smile:

Just to be explicit, this is the proposed workflow:

  • Start a process via lime test neko and leave it running
  • Copy some new assets to export/linux64/neko/bin/assets
  • Call lime update

The existing, running process will be able to get the new assets from getBitmapData et all.

Is that right?

1 Like

That’s how it should work. I’d love “lime run” to be able to call “update” automatically in the background, but this is not possible at the moment because we would need an efficient file watcher to know when a change has occurred.

Thanks, it works!

I have a file watcher. I can’t claim that it’s efficient, though, or consistent. It uses mtime to check for file changes. Perhaps I’m strange, but I have my git repositories on Dropbox shares; so I end up with strange situations (eg. changing branches resets the mtime values to the current time).

That may be okay (it is for my tiny project). But it doesn’t yet effectively monitor sub-trees of directories/files.

I found what looks like a race condition. For the record, I’m using HaxeFlixel.

My app loads an image name from a text file. If the file changes, it reloads the (JSON) data.

My workflow is:

  • Launch the game with image A in the file and in assets
  • Switch to a scene that uses the image
  • Copy a new image B into assets and run lime update neko
  • Edit the text file to reference B instead of A
  • Save and watch the scene reload

This works fine if I’m not on the scene where the image is loaded (in create, fairly early in the FlxState life cycle).

It crashes if I’m on the scene where the image is used.

I invoke lime update neko through a system.io.Process instance. Even though I consume exitCode (which blocks the current thread until the process completes), it crashes unless I put a sleep(0.75) between the call to lime update neko and reloading the scene.

If you’re interested, you can see the current state of the code here on GitHub.

@singmajesty my current directory watcher is here. It basically:

  • On load, recursively goes through all files in the target directory, and notes the mtime (modified time)
  • On a specified interval (I use one second), recalculates the mtime, and notes any new/deleted/changed files
  • If anything changed, it triggers a user-specified callback

I can’t claim that it’s efficient (I don’t know anything about the performance characteristics of FileStat and mtime on Linux or Windows). I know that it works well enough on Ubuntu/Linux (enough for my project). If you’re interested, we can discuss it further at some point.