Jump to content

Writing to and reading values from .craft file?


Recommended Posts

I'm working on a plugin that will need to utilize a name-tagging system for crew-able parts. I've looked through the kOS source code (kOS uses a right click context menu to give parts a name tag), but honestly, I can't figure out what a lot of it means, since almost none of it is commented out. There are a few things I need to do differently, anyway. I need to give parts name tags only in the editor, I need to give the editor an *additional* description-type box via a GUI, and I need my plugin to be able to read those values when the craft is loaded onto the launchpad for the first time. I'm brand new to C#, and I've been looking through the new API stickied in this sub, but I really don't know what a lot of it means since most of the entries don't have descriptions. Even the ones that do, I can't figure out how to actually write them into my code.

The way I understand it, I need to be able to write these name tags to my plugin's corresponding config node in the .craft file, and the description-type box input needs to create a field and then write a value for it at the top of the .craft file, where the vessel name and description go. So, I need to figure out how to write these values into the .craft file AND how to later read them.

Yes, yes, I need to learn more C#. I get that, but this is part of my learning. I'm at a point in the development of my plugin where I just don't know how I can progress further without having to resort to just asking somebody more experienced to just explain stuff to me—which, believe me, is super frustrating. I would really prefer to learn this stuff on my own, since you're all busy with your own plugins, and I like figuring things out on my own, but I'm starting to lose confidence in my ability to actually get this working in a reasonable amount of time.

Any and all help is greatly appreciated!

Link to comment
Share on other sites

Saving and loading Part based variables are relatively easy. Make a PartModule, add a string global variable, and tag it with KSPField attribute with "isPersistent=true" and "guiActiveEditor=true". That gives you a string on that part which is visible in the editor and will be the same value on the launchpad (after the modules .OnLoad method has run that is)

To get the value back, you can either check it inside the module or get it from any other class in your project with: aVessel.parts.Modules.GetModule<MyModule>().stringVar

Edited by Crzyrndm
Link to comment
Share on other sites

19 hours ago, Crzyrndm said:

Saving and loading Part based variables are relatively easy. Make a PartModule, add a string global variable, and tag it with KSPField attribute with "isPersistent=true" and "guiActiveEditor=true". That gives you a string on that part which is visible in the editor and will be the same value on the launchpad (after the modules .OnLoad method has run that is)

To get the value back, you can either check it inside the module or get it from any other class in your project with: aVessel.parts.Modules.GetModule<MyModule>().stringVar

Awesome. Thank you! As soon as finals are done today I'm gonna dig in and start working on that.

Quick clarification:

In the module, I could have, say, three different values

Name = value1
Description = value2
Title = value3

and call all three using 

aVessel.parts.Modules.GetModule<MyModule>().stringName
aVessel.parts.Modules.GetModule<MyModule>().stringDescription
aVessel.parts.Modules.GetModule<MyModule>().stringTitle

?

Edited by blorgon
Link to comment
Share on other sites

22 hours ago, Crzyrndm said:

That gives you a string on that part which is visible in the editor and will be the same value on the launchpad (after the modules .OnLoad method has run that is)

Another question, what about dynamically declaring those variables? Unless I'm misunderstanding you, it sounds like what you're suggesting is declaring the string in the code, and that will show up in the editor and on the pad. I need to have the user be able to write their own values. I'm still looking around (mostly unsuccessfully) for a good resource on building a GUI, so that the user can actually do this.

Link to comment
Share on other sites

Then you probably want a ConfigNode to save to/load from rather than an explicit string, or you can explicitly modify the PartModules OnSave/Onload methods to save/load a list/array/custom class that KSP might not serialise by default

[KSPField(isPersistent=true)]
public ConfigNode myVars;

// Somewhere else...
// loading
string[] stuff = myVars.GetValues();
foreach(string s in stuff)
{
 // parse the strings saved however you like 
}

//saving
myVars.Clear(); // I think that's the right method. You almost certainly want to load it all in and then recreate the serialised node whenever you save.
myVars.AddValue("key", "value"); // I don't normally bother with the key for fully custom stuff, just format the value with all the information you need

 

Edited by Crzyrndm
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...