Jump to content

ConfgNodes: why, when and how do I use them exactly?


Recommended Posts

Pretty much the title says it all.

I have a basic understanding of what they are, but for the sake of the thread, let's pretend I don't (it's not a big leap).

So, yeah:

- in what scenarios would I need to work with config nodes?

- and how do I do that?

Basically, AFAIK: a config node is, essentially, what you find in a cfg file. It's pretty much structured like an xml file and I receive one during the OnLoad event.

But what node do I get during OnLoad? How do I parse that information? And how does it work during OnSave?

Sorry for the noobness.

Link to comment
Share on other sites

Sorry, I am talking about a PartModule.

What do you mean with "I use ConfigNode directly to load/save my configuration files, which is not the same as part.cfg files." ?

Link to comment
Share on other sites

I think he is referring to those methods in PartModule.

If in a partModule, you call either

OnLoad(ConfigNode node)

OnSave(ConfigNode node)

Those methods will execute whenever KSP does a read/write to the persistence file so you can be compatible with the persistence system.

The way I have used it I create my persistence KSPFields in my partModule to create the values in the persistence.sfs file as part of the vessel.

I then use OnLoad(ConfigNode node) to call node.getValue("ValueName") and KSP handles all the background stuff to read the value from the persistent.sfs file.

I add a matching OnSave(ConfigNode node) with node.SetValue("ValueName", Value) to write the value to the persistent.sfs file.

Code:

public class ModuleAGExtData : PartModule
{
[KSPField(isPersistant = true, guiActive = false)]
public string AGXData;
}

creates the AGXData string that I want to persist.


--in the same PartModule
public override void OnSave(ConfigNode node)
{
node.SetValue("AGXData", "StringToSave");
}

saves the value to the persistent.sfs file specific to this partModule on its part on its vessel whenever KSP calls it's save routine such as when a scene changes or the QuickSave button is hit.


--in the same PartModule
public override void OnLoad(ConfigNode node)
{
ReadString = node.GetValue("AGXData");
}

pulls the saved string from the persistent.sfs file when KSP calls it's load routine.

And KSP handles all the stuff in the background for the management of the persistent.sfs file, such as deleting old data when a vessel is destroyed, all partModules assigned to that vessel are also deleted from the persistent .sfs file by KSP automatically.

Note that one big limitation of this method is that you can only save/load when KSP calls for it, you can't save/load yourself.

D.

Edited by Diazo
Link to comment
Share on other sites

What do you mean with "I use ConfigNode directly to load/save my configuration files, which is not the same as part.cfg files." ?

I load/save files directly using ConfigNode.Load()/Save(). I don't have a PartModule that does it for me, because my plugins don't have parts.

Link to comment
Share on other sites

Ah, I suppose what Blizzy is talking about also counts as ConfigNode.

That method is great for saving mod settings as it does not respect the persistence system.

First, create your .cfg file with your values, in this example I am loading TWR1.cfg from my mod's sub-directory in GameData.

public void Start()
{
TWR1Node = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/Diazo/TWR1/TWR1.cfg"); //load .cfg file
TWR1KeyCodeString = TWR1Node.GetValue("TWR1Key"); //read keybind from .cfg, no functionality to set keybind from ingame exists yet
}

Loads the key that the player has bound for controlling my Vertical Velocity mod.

TWR1Node.SetValue("TWR1Key", TWR1KeyCodeString);//set the new Keybind to the ConfigNode in memory
TWR1Node.Save(KSPUtil.ApplicationRootPath + "GameData/Diazo/TWR1/TWR1.cfg");//save the ConfigNode in memory to the .cfg file on disk.

And saving a new KeyBinding to the .cfg file on disk.

D.

Edited by Diazo
Link to comment
Share on other sites

No, you can read/write a limited number of types.

However, the KeyCode type is not supported which is why my example converts it to a string.

The official documentation states that string, bool, int, float, Vector2, Vector3, Vector4 or Quaternion data types are supports, but I have not actively tested this.

Notably from other posts I've seen, double is not supported.

D.

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