dkavolis / Ferram-Aerospace-Research

Aerodynamics model for Kerbal Space Program
Other
81 stars 32 forks source link

No lift bug (NRE in ferram4.FARControllableSurface.get_MovableSection) #136

Closed BrettRyland closed 2 years ago

BrettRyland commented 2 years ago

Hey @dkavolis, Some BDArmory users have been occasionally having issues where, after running KSP for a while, planes suddenly have no lift (the aerodynamic forces overlay shows nothing) and can't take off. I found a bunch of the following in their log:

[LOG 17:54:47.562] [FAR v0.16.0.5]: Updating vessel voxel for GreyHawk II MkXa
[EXC 17:54:47.563] NullReferenceException: Object reference not set to an instance of an object
  ferram4.FARControllableSurface.get_MovableSection () (at <1db2823b77704510ad0dd61fc9d23a0a>:0)
  ferram4.FARControllableSurface.FixedUpdate () (at <1db2823b77704510ad0dd61fc9d23a0a>:0)
  UnityEngine.DebugLogHandler:LogException(Exception, Object)
  ModuleManager.UnityLogHandle.InterceptLogHandler:LogException(Exception, Object)
  UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)

This appears to be caused by part.FindModelTransform(transformName); returning null for some part (I suspect an EVA kerbal or the parachute in their inventory, the particular plane above uses an EVA kerbal in a command seat instead of a cockpit). Adding in a null check fixes the issue, but means that part.FindModelTransform(transformName); is being called every fixedUpdate, so you may want to add a flag so that the check is only performed once and maybe give a warning in the log about the missing transform (in case the part is just badly configured and is fixable).

diff --git a/FerramAerospaceResearch/LEGACYferram4/FARControllableSurface.cs b/FerramAerospaceResearch/LEGACYferram4/FARControllableSurface.cs
index 426316c8..da6786bd 100644
--- a/FerramAerospaceResearch/LEGACYferram4/FARControllableSurface.cs
+++ b/FerramAerospaceResearch/LEGACYferram4/FARControllableSurface.cs
@@ -264,6 +264,7 @@ namespace ferram4
                 if (movableSection != null)
                     return movableSection;
                 movableSection = part.FindModelTransform(transformName); //And the transform
+                if (movableSection is null) return null;
                 if (!MovableOrigReady)
                 {
                     // In parts copied by symmetry, these fields should already be set,
dkavolis commented 2 years ago

Thanks. Animations will now be disabled for parts without valid control surface models, no repeated calls to part.FindModelTransform but animations will not be reenabled if a valid transform is added which should be never/very rarely.