On_reloaded lifecycle hook in editor?

I’m using hooks.editor_script to run scripts to rebuild some assets (generating atlases, lua modules, etc) before the game is run.

Using on_build_started is working well for this purpose, but it would be nice to be able to hot reload these changes in development.

Are there plans to add a hot reload lifecycle hook? Or any other way to achieve this?

I’ve come up with a workaround for this that suits my workflow. It’s specific to Mac OS X at the moment, but thought I’d share in this forum in case others are interested.

Background: The editor runs a HTTP server that can be hooked into to trigger a hot reload of the running game from an external command. We can use this to trigger a hot reload of our game anytime, like when we update an image or other asset externally.

The editor port is random everytime though, so we need a script to dynamically locate the port. The easiest way to find the port is to:

  • use ps to get the process ID of the defold editor
  • use lsof -p pid and look for a line ending with LISTEN, which contains the editor port (e.g. *:63801 (LISTEN) means the editor is listening on port 63801)

putting this together, we can use this snippet to find the editor port

EDITOR_PORT=$(ps -A | awk '/[c]om.defold.editor/ { print $1 }' | xargs lsof -p | grep LISTEN | grep -Eo '\*:\d+' | cut -f2 -d:)

Triggering a hot reload can now be done with:

curl -X POST localhost:${EDITOR_PORT}/command/hot-reload

In my project, i’ve combined this with watchexec to automatically trigger a rebuild and hot reload when I change any assets (you can find similar tools for other platforms).

The full script looks like:

EDITOR_PORT=$(ps -A | awk '/[c]om.defold.editor/ { print $1 }' | xargs lsof -p | grep LISTEN | grep -Eo '\*:\d+' | cut -f2 -d:)
watchexec -w assets "defold-importer assets && curl -X POST 'localhost:${EDITOR_PORT}/command/hot-reload'"

This will re-import my assets (defold-importer is a tool I created to generate guis, tilemaps, collections etc in defold format from Aseprite files), and hot reload the game whenever I change anything under the assets/ folder.

6 Likes