Reloading without restarting the game
Hosted mods and scripts can be rebuilt and swapped in while the game runs. NativeAOT mods can't, which is one more reason to develop hosted and ship NativeAOT.
Three ways to reload:
- On every build: turn on MODS > Installed mods > Platform.NET > Reload .NET mods when rebuilt. With
<FH2ModsDir>set in your project,dotnet buildcopies the mod into the game and it restarts straight away. Scripts reload when you save them. - From the console: type
reload MyMod. Leave out the name to reload every hosted mod and start any new ones. - From a key: bind Reload .NET mods on the Mods page.
What happens in a reload
- The old copy's
Unloadingtoken is cancelled and itsOnUnload()runs. Stop your threads and timers there. - The old copy's hooks are switched off, and its
Game.Tickhandlers, ANNA entries and otherFH2registrations are removed. - The new copy's
OnInit()runs, withIsReloadset totrue.
protected override void OnInit()
{
new Thread(() =>
{
while (!Unloading.IsCancellationRequested)
{
// ...
Unloading.WaitHandle.WaitOne(1000); // sleeps, but wakes up for a reload
}
}) { IsBackground = true }.Start();
}
protected override void OnUnload() => _file?.Dispose();
What a reload can't change
The platform can't remove things a mod added, so:
- Settings, pages, title-screen entries, hotkeys and commands the new copy adds with the same key keep their place and call the new code.
- Ones it adds for the first time only show from the next launch.
- Ones it no longer adds stay until the game restarts, and do nothing.
- Text and database edits take effect at the next launch, because the game reads them once.
The old copy stays in memory until the game closes, so reloading is for development, not for players.
Mods that can't be reloaded
Some mods change the game as it loads: what they did is in the game's data and in the old copy's memory, and a fresh copy can't pick it up. Such a mod says so, and a reload (A on the Hosted .NET mods page, the reload key, reload, auto-reload) then leaves it running, with a line in the log saying a restart is needed for the new build:
public override bool Reloadable => false;
Public Overrides ReadOnly Property Reloadable As Boolean
Get
Return False
End Get
End Property
The console
Press ~ (the key left of 1) to open or close it. It shows platform.log as it's written, with failures in red and warnings in yellow. It also has a prompt:
| Type | To |
|---|---|
list | list every mod's commands |
MyMod speed | run your mod's speed command |
reload MyMod | reload a hosted mod |
mods | list the hosted mods |
log off | hide the log: only what you type and the replies show |
log MyMod | show only the log lines with that text (your mod's, say) |
log on | show the whole log again |
cls, help | clear the screen, show help |
The console remembers what log was set to, in FH2Console.ini beside it. log is new in 0.12.0.
The console is a separate program (fh2-mods\dotnet\FH2Console.exe), so closing it can't close the game.
Logging
Log("...")writes a line toplatform.log, tagged with your mod's name.Console.WriteLine,Trace.WriteLineandDebug.WriteLinego there too. They're safe to call anywhere, hooks included.
Watching values
Add a command, then type it in the console:
AddCommand("speed", "my car's speed", _ => $"{Player.Car?.SpeedKmh:F0} km/h");
Or show values over the game while you drive with DebugOverlay.
Breakpoints
Attach Visual Studio to azimuth.exe (Debug > Attach to Process), with code type Managed (.NET Core, .NET 5+). Hosted mods load with their .pdb, so breakpoints in your code are hit in the running game, after reloads too.
A breakpoint pauses the game's thread. Don't stop on one in Game.Tick while you're racing.
