Editor needs to build twice to reload scripts generated by lifetime hook

Hello guys,

My workflow is as follows:

  • Write Haxe code in an external editor.
  • Compile Haxe code with hxdefold, which generates .script files.
  • Use .script files in Defold.

And I have an on_build_started hook which runs the Haxe compiler, so for the most part it works fine, I only need to run the game in the editor. However I’m dealing with this annoying thing where changes to scripts need a second run in order to be reloaded, as you can see in the gif below.

As you can see, I add a line to the character’s script so that he deletes himself, but it only comes into effect the second time I run the game. I have also confirmed that the hook runs and compiles Haxe on the first run, the generated lua already has the deletion line.

Also, if I disable the editor hooks script and build my Haxe outside of Defold before running, the problem becomes sporadic. Sometimes script changes will have effect immediately and sometimes they won’t.

My hooks.editor_script:

local M = {}

function M.on_build_started(opts)
    return {
      {
        action = "shell",
        command = { "haxe", "build.hxml" }
      }
    }
  end

return M
2 Likes

Does the editor properly detect the changes made to the script files when regenerated using Haxe? The editor should do this and evict the build cache and on the next build you should get a full and clean rebuild.

I’m not sure how to check this, but actually from what you described I think I have an idea. Basically, the .script files don’t actually change when I’m updating the Haxe code. This is because Haxe actually compiles everything into a single lua file, which in my case is named main.lua and sits at the Defold project root. Then all of the scripts are basically generated once by hxdefold and have their main callback functions like init and so on forwarded to their respective implementations in main.lua. So I’m guessing that since the script files don’t change, the editor reuses main.lua from cache?

If that’s the case, is there something I could do in the build hook to force Defold to reload it? Maybe append some randomized comment at the end of a script file or something like that? :smiley:


Edit: The scripts actually don’t remain entirely untouched. They are all deleted and re-generated each time. Mentioning it in case it matters.

Ok, but if this file changes then the editor should detect it and use the new version.

@britzl I’m still having this issue by the way :slight_smile:

Could it be that since when on_build_started is called, the lua/script files are all still identical to the last run, the editor assumes it doesn’t have to build them?

Not sure, let me check.

1 Like