Jump to content

PartModule.OnLoad() is schizophrenic! Here's how to fix it.


Recommended Posts

I was trying to implement fancy module config nodes the way stock modules have them, like the PROPELLANT blocks in ModuleEngines. I have no idea why, but it seems near impossible to get OnLoad to cooperate with us on our own modules. It works fine in the VAB, but as soon as you enter a flight scene, the ConfigNode you get is the one from the Vessel, not the part configuration. For many things, this is completely useless.

So, after much toiling and consumption of all my remaining alcohol, here's a one-liner:

if (part.partInfo != null) { node = GameDatabase.Instance.GetConfigs("PART").Where(c => part.partInfo.name == c.name.Replace('_', '.')).Single().config.GetNodes("MODULE").Where(n => n.GetValue("name") == moduleName).Single(); }

This grabs the part config directly, locates your module (note: only works if there is exactly one of your module on the part, sucky limitation) and replaces the parameter config node with what it finds. It could use improvement and you might tweak it to your own purposes, but I figured I'd share anyway.

Link to comment
Share on other sites

I was trying to implement fancy module config nodes the way stock modules have them, like the PROPELLANT blocks in ModuleEngines. I have no idea why, but it seems near impossible to get OnLoad to cooperate with us on our own modules. It works fine in the VAB, but as soon as you enter a flight scene, the ConfigNode you get is the one from the Vessel, not the part configuration. For many things, this is completely useless.

So, after much toiling and consumption of all my remaining alcohol, here's a one-liner:

if (part.partInfo != null) { node = GameDatabase.Instance.GetConfigs("PART").Where(c => part.partInfo.name == c.name.Replace('_', '.')).Single().config.GetNodes("MODULE").Where(n => n.GetValue("name") == moduleName).Single(); }

This grabs the part config directly, locates your module (note: only works if there is exactly one of your module on the part, sucky limitation) and replaces the parameter config node with what it finds. It could use improvement and you might tweak it to your own purposes, but I figured I'd share anyway.

You should ditch the 'where' calls. :) Other than that, there are really only other minor optimizations to make to the 'one-liner' lol.

Thank you for sharing!

if (part.partInfo != null)
{
node =
GameDatabase.Instance.GetConfigs("PART")
.Single(c => part.partInfo.name == c.name.Replace('_', '.'))
.config.GetNodes("MODULE")
.Single(n => n.GetValue("name") == moduleName);
}

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...