Project

General

Profile

Bug #1265

Cannot bind static callback to one of the GameEvents

Added by Faark over 7 years ago. Updated almost 5 years ago.

Status:
Needs Clarification
Severity:
Low
Assignee:
-
Category:
Plugins/Add-Ons
Target version:
-
Start date:
09/03/2013
% Done:

0%

Version:
Platform:
Any
Expansion:
Language:
English (US)
Mod Related:
No
Votes:
Arrow u r green
Arrow d r red

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 5 years ago

  • Status changed from New to Needs Clarification

Also available in: Atom PDF