Hot-Swappable Game DLL
Eclipsed Engine
Restarting the editor after every code change kills iteration speed. I built the system that lets Eclipsed Engine's editor recompile and reload the game's code with one button, without restarting.

At a glance

Why a DLL?

In Eclipsed Engine the game is not part of the editor. The editor is one program, and all of the game code is compiled into its own dynamic library that the editor loads at startup.

Code that is compiled into the editor can't be changed while the editor is running. A library can be unloaded and loaded again, so the editor can throw away the old game code and pick up the new code without restarting.

Loading a game the editor knows nothing about

The editor has no idea which components the game contains or what they're called. The only thing it relies on is one function that every game library exports: RegisterReflectedTypes.
bool GameModuleManager::Load()
{
    gameModule = LoadLibraryA(dllPath.generic_string().c_str());
    if (!gameModule)
        return false;

    registerReflectedTypesFunc = (Func)GetProcAddress((HMODULE)gameModule, "RegisterReflectedTypes");
    if (!registerReflectedTypesFunc)
        return false;

    registerReflectedTypesFunc();

    return true;
}
Nobody writes that function by hand. My reflection system generates it, and it registers every component in the game. Once it has run, the editor can create the game's components, show them in the inspector and save them, all without including a single game header. This keeps the boundary between editor and game clean.

Recompiling safely

Pressing Recompile in the editor runs this:
bool GameModuleManager::Reload()
{
    SceneManager::UnloadActive();
    const bool ok = TryCompileAndLoad();

    SceneManager::ReloadActive();

    return ok;
}
The order matters. Every game component in the scene runs code that lives inside the library. If the library were unloaded while those components were still alive, they would point at code that no longer exists and the editor would crash. So the scene is unloaded first.

With the scene gone, the library can be swapped safely:
bool GameModuleManager::BuildDLL()
{
    if (IsLoaded())
        Unload();

    bool wasChanged = HasChanged();

    if (wasChanged && !RunHeaderTool())
        return false;

    if (wasChanged && !GenerateCmake())
        return false;

    if (wasChanged && !Compile())
        return false;

    if (!CopyDllFile())
        return false;

    return true;
}
  1. Unload: the old library is freed.
  2. HeaderTool: the game's headers are scanned and the reflection code is regenerated, so new components and fields are picked up.
  3. CMake: the project is regenerated, so new source files are included.
  4. Compile: the game is built into a new library.
  5. Copy: the new library is copied next to the editor.

The new library is then loaded, its types are registered and the scene is loaded again.

The copy step solves a Windows-specific problem. Windows locks a DLL for as long as it is loaded, so the compiler couldn't overwrite it. The editor loads a copy instead, which leaves the original free for the next build.

Skipping work that isn't needed

A full build takes time, and most of the time the editor starts, nothing has changed. Before building, the editor compares the library's last write time with every file in the project's Content folder. If no file is newer, the three build steps are skipped and the existing library is loaded straight away.
for (auto& entry : fs::recursive_directory_iterator(PathManager::GetProjectRoot() / "Content"))
{
    if (!entry.is_regular_file())
        continue;

    if (fs::last_write_time(entry.path()) > dllLastWriteTime)
        return true;
}

return false;

Surviving a failed build

Game code will have compile errors, and the editor has to survive them. Each build step runs as its own process and writes its output to a log file. If a step fails, the editor prints an error with the path to the log and records that the game has build errors.

The editor keeps running without a game library and blocks only the actions that would break without one: entering play mode and saving the scene. Once the code is fixed and recompiled, everything is unlocked again. A typo costs you a recompile, not a crashed editor or a corrupted scene.

Trade-offs and next steps

I kept the first version simple and reliable. These are its known limits and how I'd address them:
  • It reloads, it doesn't live-patch. The scene is reloaded from its saved state, so unsaved changes are lost. Serializing the scene to memory before unloading, using the same reflection data, would keep them.
  • The editor waits for the build. The build blocks the editor. Running it on a background thread and swapping when it finishes would let you keep working.
  • Windows only. Loading uses the Win32 API directly. Putting it behind a small platform layer with dlopen/dlsym would add Linux and macOS support.

The source code is on GitHub.