Jump to content

part.AddModule(ConfigNode node): NullReferenceException in PartModule.Load(node)-help


Recommended Posts

So yeah. I'm trying to dynamically add PartModules to a part in the VAB editor.

When I call part.AddModule(ConfigNode node) with a known good ConfigNode (it's an EngineModule from another engine), PartModule.Load(node) causes a NullReferenceException.

What am I doing wrong?

Link to comment
Share on other sites

Update: After lots of blind flailing and remembering a bit of a conversation with Mu, I've discovered that the problem is that part.AddModule(ConfigNode node) fails if the partModule's .Awake() method doesn't get called (apparently there's setup that happens in there).

Unfortunately, PartModule.Awake() is private.

Fortunately, C# has reflection.

I've now managed to get new PartModules to successfully spawn from the editor. However, the parts they spawn on still do not appear to recognize their PartModules.

What is the order of operations necessary to add a new module to a part at runtime, from outside that part's scope?

Link to comment
Share on other sites

Nevermind! I got it to work!

For everyone who needs to add a PartModule to a part in the VAB, here is how you do it:


PartModule module = part.AddModule (addNode.GetValue("name"));
if(Awaken (module)) { // uses reflection to find and call the PartModule.Awake() private method
module.Load(addNode);

Awaken(module) is a static method that looks like this:



public static bool Awaken(PartModule module)
{
// thanks to Mu and Kine for help with this bit of Dark Magic.
// KINEMORTOBESTMORTOLOLOLOL
if (module == null)
return false;
object[] paramList = new object[] { };
MethodInfo awakeMethod = typeof(PartModule).GetMethod("Awake", BindingFlags.Instance | BindingFlags.NonPublic);

if (awakeMethod == null)
return false;

awakeMethod.Invoke(module, paramList);
return true;
}

Because reflection in C# is AWESOME.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 years later...

Careful, adding a PartModule this way CAN NOT save/load data.

When a vessel loads, only PartModules in the part prefab are present when KSP loads from the save file. It is only after the actual load happens that your own code runs to add the PartModule back onto the part.

So because the partModule is added back in after the Load operation takes place, it reverts to defaults and never loads data from disk.

That is okay if your partModule doesn't need to save/load data, otherwise you need to use ModuleManager to add your partModule to the part prefab.

(This was true until KSP 1.0.5, I am not aware of anyone having tested this on KSP 1.1 yet.)

D.

 

edit: Hmmm, opening post is from 2013, differing versions of KSP may be an issue here.

Edited by Diazo
Link to comment
Share on other sites

Hello!

(Sorry I couldn't answer, because I was on a holiday)

On 2016. 04. 14. at 9:12 PM, sarbian said:

Use ModuleManager and don't a necro 3 year old post...

I would use it, but I need to add modules in-flight.

BTW it works without calling the awake method(as far as I know that's called during the flight screen). This is a more or less working code:

part.AddModule(addNode);

...more or less, because some modules cannot be added whit this method like science experiment module and science lab module.Whit those I always get a Null Reference Exception.

Link to comment
Share on other sites

On 2016. 04. 14. at 6:35 PM, Diazo said:

Careful, adding a PartModule this way CAN NOT save/load data.

When a vessel loads, only PartModules in the part prefab are present when KSP loads from the save file. It is only after the actual load happens that your own code runs to add the PartModule back onto the part.

So because the partModule is added back in after the Load operation takes place, it reverts to defaults and never loads data from disk.

That is okay if your partModule doesn't need to save/load data, otherwise you need to use ModuleManager to add your partModule to the part prefab.

(This was true until KSP 1.0.5, I am not aware of anyone having tested this on KSP 1.1 yet.)

D.

 

edit: Hmmm, opening post is from 2013, differing versions of KSP may be an issue here.

Yes, I know that and I made a simple system that handles the saving and loading the added modules.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...