Jump to content

KSPField, PartModule, and... Clones?


Recommended Posts

I've been writing a plugin for some time now, and I want to configure things like the power usage of a part from the part module definition.


PART
{
//Some Part
MODULE
{
name = ModuleKSPFieldTest
powerConsumption = 65
}
}

The module looks something like this:


public class ModuleKSPFieldTest : PartModule
{
[KSPField(guiActive=true)]
public float powerConsumption = 0f;
}

The problem is, as far as I can tell, that the only instance of ModuleKSPFieldTest that recieves this data is the one that's loaded at startup (and filed away somewhere?), and any parts that actually use that module in flight only display the compile time default of 0. Does this have anything to do with the fact that the debug console displays the part as Part(Clone)?

Arodang has asked for help about this before, and as far as I can tell hasn't even been acknowledged, so I'm making this a proper post.

Link to comment
Share on other sites

You usually load your part with all PartModules from a config, so it ends up in PartLoader.LoadedPartsList. All actual instance are clones from those and should also get the same values... as far as i understand KSP^^

Link to comment
Share on other sites

I was trying to figure out everything that I could about KSPFields and how they work so that I could explain it to others. I am working on a set of examples that I hope others will find helpful: http://forum.kerbalspaceprogram.com/threads/56053-Example-plugin-projects-to-help-you-get-started

Take a look at https://github.com/taraniselsu/TacExamples/blob/master/02-SimplePartModule/Source/SimplePartModule.cs where I worked out the object life cycle in KSP/Unity. And look at https://github.com/taraniselsu/TacExamples/blob/master/05-KspFields/Source/KspFieldsModule.cs where I am figuring out how KSPFields work -- not finished though.

I did find that KSPFields declared as double's and long's do not work, only int's, float's, string's, and bool's. I also cannot get MyType to work even though it should :(.

I don't see anything wrong with the code you posted, so I would add more logging to see what is happening.

... All actual instance are clones from those and should also get the same values...

Emphasis is mine because I have had many issues with values not being copied from the prefab part to the clones :(

Link to comment
Share on other sites

It seems to be working (I used a double by mistake) on the primitive types, but IConfigNode implementing classes are still only being loaded once. I worked around this by copying the data manually from part.partInfo.partPrefab, which is the original part loaded by the PartLoader, and whose modules can be searched for the original IConfigNode implementing classes. You may want to make a copy of the data if you plan on changing it, since unless it's a struct, the copied objects are references.

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