Skip to content

Commit 1d523f9

Browse files
authored
Add RestoreMaxPhysicsDT (#95)
When using physics warp, Unity will set the max physics dt to be at least as high as the scaled physics dt. But KSP will never restore it back to the normal value from the settings. This can degrade performance as it allows more FixedUpdates to run per frame. reported by Wilds on the forum: https://forum.kerbalspaceprogram.com/index.php?/topic/184740-solved-losing-300-performance-after-time-warping/
1 parent 9c5c432 commit 1d523f9

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

GameData/KSPCommunityFixes/Settings.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ KSP_COMMUNITY_FIXES
292292
// speed up reading) at the cost of human readability.
293293
SkipIndentsOnSavesAndCraftFiles = false
294294

295+
// After using physics warp, Unity's max physics dt will never be returned to the value specified in
296+
// game settings which can degrade performance in some cases
297+
RestoreMaxPhysicsDT = true
298+
295299
// ##########################
296300
// Modding
297301
// ##########################
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using HarmonyLib;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using UnityEngine;
8+
9+
namespace KSPCommunityFixes.BugFixes
10+
{
11+
public class RestoreMaxPhysicsDT : BasePatch
12+
{
13+
protected override void ApplyPatches(List<PatchInfo> patches)
14+
{
15+
patches.Add(new PatchInfo(
16+
PatchMethodType.Prefix,
17+
AccessTools.Method(typeof(TimeWarp), "updateRate"),
18+
this));
19+
}
20+
21+
static void TimeWarp_updateRate_Prefix()
22+
{
23+
// when you use physics warp, it increases Time.fixedDeltaTime
24+
// Unity will internally increase maximumDeltaTime to be at least as high as fixedDeltaTime
25+
// But nothing in the KSP code will ever return maximumDeltaTime to the value from the settings when time warp is over
26+
// Setting it just before TimeWarp sets fixedDeltaTime guarantees that all points of code will have the correct value
27+
Time.maximumDeltaTime = GameSettings.PHYSICS_FRAME_DT_LIMIT;
28+
}
29+
}
30+
}

KSPCommunityFixes/KSPCommunityFixes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<Compile Include="Modding\ModUpgradePipeline.cs" />
105105
<Compile Include="Performance\AsteroidAndCometDrillCache.cs" />
106106
<Compile Include="BugFixes\DoubleCurvePreserveTangents.cs" />
107+
<Compile Include="BugFixes\RestoreMaxPhysicsDT.cs" />
107108
<Compile Include="Performance\CommNetThrottling.cs" />
108109
<Compile Include="Performance\DisableMapUpdateInFlight.cs" />
109110
<Compile Include="Performance\MemoryLeaks.cs" />

0 commit comments

Comments
 (0)