AoE4 mod development: Reloading a SCAR script in-game without restarting

Hi everyone,

I’m developing an AoE4 custom scenario and have been using loadfile() to reload its SCAR script from a local project folder while the game is running.

This has helped me iterate on script logic and a custom XAML panel without restarting the game for every change. Here is the workflow that worked in my setup.

1. Launch AoE4 with -dev and start your custom scenario

The scenario still needs to be built and loaded normally to begin with.

Keep an editable copy of its .scar source in a local folder. For example:

D:/aoe4_dev/my_scenario.scar

The path must point to an existing file on your computer.

2. Add a development loader to that source file

ReloadPrototypeScar() is a custom function, not a built-in AoE4 command. This is a simplified version:

function ReloadPrototypeScar()
    local sourcePath = "D:/aoe4_dev/my_scenario.scar"

    local loadOk, chunk, loadError = pcall(loadfile, sourcePath)

    if not loadOk then
        print("[Dev Reload] loadfile failed: " .. tostring(chunk))
        return false
    end

    if chunk == nil then
        print("[Dev Reload] Could not load source: " .. tostring(loadError))
        return false
    end

    local runOk, runError = pcall(chunk)

    if not runOk then
        print("[Dev Reload] Script execution failed: " .. tostring(runError))
        return false
    end

    print("[Dev Reload] SCAR source reloaded.")
    return true
end

Replace the example path with your own absolute path. I use forward slashes.

3. Make sure you are using the SCAR console

This caused some confusion during my testing.

In my setup, Ctrl+Shift+~ opens/closes the console, and Alt+Shift+~ changes the console context. Check the prompt and switch until it says SCAR.

My scenario functions were not available in the Global context. Entering the command there did not reload my scenario.

You can check whether the helper is available in the current context:

print(type(ReloadPrototypeScar))

It should print function. If it prints nil, the helper is not defined in that context yet.

4. Bootstrap the script from disk

The sequence that first worked for me was:

assert(loadfile("D:/aoe4_dev/my_scenario.scar"))()

Then:

ReloadPrototypeScar()

The first command loads and executes the external source file, which also defines the loader function in the SCAR context. The second command calls that loader.

This does not mean both commands are always required. The direct loadfile() command is useful when the running scenario does not have the helper yet, or has an older version with an incorrect source path.

Once the correct helper is available, my usual edit loop is:

  1. Edit and save the external .scar file.
  2. Run ReloadPrototypeScar() in the SCAR console.
  3. Check the new console output and try the changed behavior.

For these script edits, I do not rebuild the packaged mod each time.

Refreshing UI and preserving state

Reloading SCAR does not automatically refresh an existing XAML panel. In my full implementation, I remove the old panel and recreate it after a short delay. The simplified loader above only reloads the script; it does not include that UI refresh logic.

Also, executing a whole source file again runs its top-level code again. Initialization, rules and callbacks need to be managed so they do not reset the current session or register twice.

This workflow reloads SCAR code. Changes to the map, blueprints or other packaged assets may still require rebuilding and starting a new session.

The absolute-path loader is for local development and should be removed or disabled before publishing the mod.

Has anyone used a similar workflow? I would be interested in hearing how other modders handle SCAR reloads, runtime state and XAML panel refreshes.

1 Like

Thank you very much for sharing this great technique. Being able to test some things now without having to rebuild will be very usefull. Great find.

Got the loadfile() loop working the same way.

One thing worth flagging for anyone whose scenario is just like mine split across multiple .scar files via import(): if your entry script imports a bunch of module files, reloading the entry script re-executes all of those imports too, not just the file you changed. Two consequences I hit:

  1. Any top-level Rule_Add* or UI_AddChild calls that live outside a function (not inside a _Start()/_Init() you call once) get re-registered on every reload — duplicate rules/intervals stacking up.
  2. Worse: top-level state tables (e.g. my_state = {}) get reset to empty every time the file reloads, silently wiping whatever your test session had accumulated.

So for a multi-module setup I’d reload individual module files on their own, not the entry script point loadfile() at just the file you’re actively editing. Still a huge win over restarting the game for every change, just worth the extra care once the project isn’t a single-file script anymore.

To answer your question about other modders’ workflows: can’t speak for others, but on runtime state specifically, I ended up with a small helper that snapshots a named list of top-level variables before a reload and restores them after so a module keeps its accumulated test-session state across edits instead of resetting to its initializer every time. You register which variables a module owns once per session, then each reload preserves exactly those.

For XAML panels: build the panel’s XAML via UI_AddChild exactly once, guarded by a flag, and every later update only calls UI_SetDataContext on the existing panel, no remove, no rebuild. Skip even that call when nothing’s actually changed, by comparing a cheap signature of the fields that matter against the last one sent. UI_Remove is reserved for genuinely hiding the panel, not routine refreshes. A panel module that does UI_Remove then UI_AddChild each time it (re)shows survives a live loadfile() reload cleanly too, no duplicate panel, no broken bindings.