# On\_reloaded lifecycle hook in editor?

**URL:** <https://forum.defold.com/t/on-reloaded-lifecycle-hook-in-editor/75667>\
**Category:** Questions\
**Created:** [January 13, 2024, 10:52pm UTC](https://forum.defold.com/t/on-reloaded-lifecycle-hook-in-editor/75667 "2024-01-13T22:52:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranavraja](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pranavraja/32/33418_2.png) [@pranavraja](https://forum.defold.com/u/pranavraja)\
**Post date:** [January 13, 2024, 10:52pm UTC](https://forum.defold.com/t/on-reloaded-lifecycle-hook-in-editor/75667/1 "2024-01-13T22:52:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pranavraja](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pranavraja/32/33418_2.png) [@pranavraja](https://forum.defold.com/u/pranavraja)\
**Post date:** [January 26, 2024, 2:16am UTC](https://forum.defold.com/t/on-reloaded-lifecycle-hook-in-editor/75667/2 "2024-01-26T02:16:53Z")

</div>

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

```sh
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:

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

```

In my project, i’ve combined this with [watchexec](https://watchexec.github.io/) 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:

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