Insidious NRE on changing scenes
Lisias opened this issue · 4 comments
While debugging net-lisias-ksp/AviationLights#4 , I found this happening on TweakScale:
[ERR 02:01:55.263] Exception handling event onGameSceneLoadRequested in class GameEventEditorListener:System.NullReferenceException: Object reference not set to an instance of an object
at TweakScale.Features.AutoScale.set_Active (Boolean value) [0x00000] in <filename unknown>:0
at TweakScale.Features.ResetOnNew.Execute () [0x00000] in <filename unknown>:0
at TweakScale.GameEventEditorListener.ResetOnNew () [0x00000] in <filename unknown>:0
at TweakScale.GameEventEditorListener.OnGameSceneLoadRequested (GameScenes data) [0x00000] in <filename unknown>:0
at EventData`1[GameScenes].Fire (GameScenes data) [0x00000] in <filename unknown>:0
[EXC 02:01:55.265] NullReferenceException: Object reference not set to an instance of an object
TweakScale.Features.AutoScale.set_Active (Boolean value)
TweakScale.Features.ResetOnNew.Execute ()
TweakScale.GameEventEditorListener.ResetOnNew ()
TweakScale.GameEventEditorListener.OnGameSceneLoadRequested (GameScenes data)
EventData`1[GameScenes].Fire (GameScenes data)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
KSPe.Util.Log.UnityLogDecorator:UnityEngine.ILogHandler.LogException(Exception, Object)
ModuleManager.UnityLogHandle.InterceptLogHandler:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
EventData`1:Fire(GameScenes)
HighLogic:SetLoadSceneEventsAndFlags(GameScenes, Boolean)
HighLogic:LoadScene(GameScenes)
FlightDriver:StartAndFocusVessel(String, Int32)
KSP.UI.Screens.SpaceTracking:FlyVessel(Vessel)
KSP.UI.Screens.SpaceTracking:BtnOnClick_FlySelectedVessel()
UnityEngine.EventSystems.EventSystem:Update()
[LOG 02:01:55.267] [UIMasterController]: HideUI
[LOG 02:01:55.369] [DistantObject] DETAIL: Scene will change from TRACKSTATION to FLIGHT
[LOG 02:01:55.369] [HighLogic]: =========================== Scene Change : From TRACKSTATION to FLIGHT (Async) =====================
Don't know why, don't know how. But this needs to go away.
This is weird.
The NRE is happening on the AutoScale
's set_active
attribute, but this thing essentially sets a internal variable and callls save
.
The save
method, by it's turn, makes use of HotkeyManager.Instance.Config
attribute, that by it's turn should be filled on the TweakScale.HotkeyManager
's awake
, that should be always executed when the thing is created the first time the Main Menu is shown.
The DATA.PluginConfiguration.CreateForType<TweakScale>();
used by the awake
is guaranteed to return a useable ConfigNode
.
Nah. Need more coffee.
The problem is that AutoScale
's set_active
is trying to access the attribute hotkeyable
before it's being initialised.
The problem is that TweakScale.OnGameSceneLoadRequested
(the one on EditorHelper
) is not guaranteeing that TweakScale is fully initialised before calling ResetOnNew
.
As a matter of fact… WHY IN FSCKING HELL I'm calling ResetOnNew
when the current scene is FLIGHT???? o.O Geez...
Anyway, fixed on commit aed3fa7 .