Auto-reloading assets

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Hugues Ross
Party member
Posts: 112
Joined: Fri Oct 22, 2021 9:18 pm
Location: Quebec
Contact:

Auto-reloading assets

Post by Hugues Ross »

Hey guys, I just started working on a new project and I wanted to share a neat little thing I did to automatically export and reload my assets. I imagine most folks know how to do this already, but I figured it could be interesting for anyone who doesn't know.



I have a full write-up of how it works with a couple code samples on my blog, but here's the gist:
  • I have an optional lua file wrapping inotify, which is not present in packaged builds
  • I scan my asset directories at a regular interval for certain file events (move/rename/change), and reload my assets when changes to them are detected
  • To maintain "safe" refs I wrap the loaded data in a table. That table can be referenced freely, since the data inside is swapped out by the reload
  • Since Tiled's lua export is manual, I also used filesystem notifications in a separate asset builder process to automatically trigger the export when the source files change. That way, I don't have to re-export my maps or track them in git.
All in all, it's pretty simple and not necessarily tied to LÖVE, but I figured not everyone knows these kinds of little tricks so it might be good to share. I'm not looking to turn this into any sort of public tool or library (hence why this is in General and not Libraries and Tools), but hopefully the explanations and code samples are enough to help you use this in your own projects! I'm also happy to answer any questions as best I can :)
User avatar
dusoft
Party member
Posts: 655
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Auto-reloading assets

Post by dusoft »

Is this hotswap useful for anything else except testing / debugging?
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

Re: Auto-reloading assets

Post by Gunroar:Cannon() »

That's cool. I kind of have something hotswap like with lua files, where I have textinput bar at the top that runs lua code and when I need to reload I do "rc(<Class>)"(rc's shortcut for function reloadClass).
But mine's just simple, not as useful as what you've got. :ultrashocked:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Ross
Party member
Posts: 100
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Auto-reloading assets

Post by Ross »

Pretty cool! Fun to see it update.
Since Tiled's lua export is manual
I use a very simple extension for Tiled that uses the `assetSaved` signal to call `tiled.trigger("Export")`. So it automatically re-exports every time I save (plus some extra stuff to have a menu toggle, so I don't have to have to have it enabled all the time).
User avatar
Hugues Ross
Party member
Posts: 112
Joined: Fri Oct 22, 2021 9:18 pm
Location: Quebec
Contact:

Re: Auto-reloading assets

Post by Hugues Ross »

dusoft wrote:Is this hotswap useful for anything else except testing / debugging?
That's the main use, but I think it's a good one depending on how much content you plan to put in a game. In my case I'm building an RPG, so being able to tweak maps, numbers, dialogue, etc on the fly will be invaluable. On a similar note, this also gives a view of what a level will actually look like to the player if a game has post-processing / lighting / other effects, or if you want to verify how the environment looks when the view is limited to the in-game camera.

Depending on how narrow your definition of "testing" is, one other benefit is the ability to have a very basic form of graphical editor for...anything preview-able really. If I want to make an effect for instance, as long as effects are derived from data I just need the engine to play it looped as I work in a text editor. Normally you'd either need to manually trigger a reload or set up a GUI for editing, but with hotswapping I can just work in one window and see the effect reflected in the other.

Ross wrote:I use a very simple extension for Tiled that uses the `assetSaved` signal to call `tiled.trigger("Export")`. So it automatically re-exports every time I save (plus some extra stuff to have a menu toggle, so I don't have to have to have it enabled all the time).
Good to know for the future! Tiled extensions are one thing I want to look into eventually. I decided against it this time since I was trying to get something done quickly and I don't know the API, but I'm definitely going to play around with it sometime.
User avatar
dusoft
Party member
Posts: 655
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Auto-reloading assets

Post by dusoft »

Hugues Ross wrote: Sat Jan 29, 2022 10:32 pm
dusoft wrote:Is this hotswap useful for anything else except testing / debugging?
That's the main use, but I think it's a good one depending on how much content you plan to put in a game. In my case I'm building an RPG, so being able to tweak maps, numbers, dialogue, etc on the fly will be invaluable. On a similar note, this also gives a view of what a level will actually look like to the player if a game has post-processing / lighting / other effects, or if you want to verify how the environment looks when the view is limited to the in-game camera.
I see, I checked your video that explains it well. If you are working with large maps and rebuilding manually would take unnecessary time it totally makes sense. I am mostly working with limited set of assets, so I didn't see point. But for your case definitely.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests