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;
}
- Unload: the old library is freed.
- HeaderTool: the game's headers are scanned and the reflection code is regenerated, so
new components and fields are picked up.
- CMake: the project is regenerated, so new source files are included.
- Compile: the game is built into a new library.
- 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.