Bug #1265
Cannot bind static callback to one of the GameEvents
Status:
Needs Clarification
Severity:
Low
Assignee:
-
Category:
Plugins/Add-Ons
Target version:
-
Start date:
09/03/2013
% Done:
0%
Language:
English (US)
Mod Related:
No
Votes:
Description
This appears to be a tiny bug deep within the internals of KSP. An exception does occur, when you try to bind any static method (or lambda) to any of the GameEvents.
System.NullReferenceException: Object reference not set to an instance of an object at EventData`1+[Game]..ctor (.OnEvent ) [0x00000] in <filename unknown>:0 at EventData`1[Game].Add (.OnEvent evt) [0x00000] in <filename unknown>:0 at ...
Here is some example code for both a working and a not working case:
public class MyConfig { private static MyConfig mInstance = new MyConfig(); private static void StaticSave(Game g) { } private void SaveNonStatic(Game g) { } static MyConfig() { // This one adds a non-static callback and works just fine. GameEvents.onGameStateSaved.Add(mInstance.SaveNonStatic); // Here we have a static one, that will compile fine but throws a NullReferenceException at runtime. GameEvents.onGameStateSaved.Add(StaticSave); } }
Yes, it is an issue that does not affect ordinary players. But fixing it should be very easy, take way less time than writing this report and will likely save some dev or other mod developers from some headaches.
History
#1 Updated by TriggerAu almost 8 years ago
- Status changed from New to Needs Clarification