Jump to content

Reading .cfg fields


Recommended Posts

I'm trying to write a wimple plugin that lets me reset goo containers and science bays at the cost of a resource, but I'm not sure how to access the fields in a module. For example if i have a module

PART{
foo = 7
MODULE
{
bar = 8
}
}

I am not sure how to access the field bar from my code.

Link to comment
Share on other sites

You do that using KSPField, as such:


[KSPField]
public float foo;

A KSPField is then usable as a standard variable in the rest of your code.

You can of course bypass this to avoid having values intended for displaying info only being changed by players.

More info in this thread, Example #1 should give you all the info you need: http://forum.kerbalspaceprogram.com/threads/92643-Official-PartModule-Documentation

Link to comment
Share on other sites

When does your code run?

Since all those fields get loaded by the module(s) in question, you probably will want to get the some_other_module PartModule on the part you're on and get the value from there, rather than going back to the original confignode it was loaded from.

Link to comment
Share on other sites

I don't know why i'm so terrible at stating my questions:P. What I really want to know is when loading config fields with [KSPFields] does the fields location matter? for example if i have a config like this:


PART
{
cost = 4
MODULE
{
name = my_module
foo = 5
}
MODULE
{
name = other_module
bar = 9
}
}

will:


[KSPField]
float cost;

[KSPField]
float foo;

[KSPField]
float bar;

work.

or do I have to do something different to access cost and bar?

my plugin loads in the flight scene and what do you mean by "get the some_other_part_module". Sorry if the questions i'm asking are obvious but i'm very new to c# and ksp modding so any help is greatly appreciated

Edited by peachoftree
Link to comment
Share on other sites

ok. so is there no way to access the aforementioned bar and cost feilds? the mod im working on adds a mechanism to reset science bays and goo canisters with a resource and eva. Similar to dang it's method of repairing parts with spares, except i'm reseting science experiments with cleanout kits. I need to acess the Inoperable feild in ModuleScienceExperiment but have not found a way to do that.

Edited by peachoftree
Link to comment
Share on other sites

All KSPFields are public and have methods for altering them, so you just need to access the Part that your PartModule is attached to or some other PartModule attached to the same Part.


/* From within your part module */

//Set the cost field to 5000
part.Fields.SetValue("cost", 5000);

//Set the other modules bar value to 10
PartModule pm = part.FindModulesImplementing<other_module>();
other_module.Fields.SetValue("bar", 10);

I've never actually tried to do this before, but it seems like it should work. There are also GetValue and ReadValue methods, and overloads to specify the type the field is using.

Accessing the other PartModule assumes that you actually have access to its Type, if it's from some other mod then you'll have to use some other method to assign the PartModule before you can get to its fields.

Link to comment
Share on other sites

KSPFields are usually just members, and as DMagic said are public. If you already are referencing whatever defines OtherModule (let's say it's a stock Squad module like ModuleParachute)

ModuleParachute otherModule = (ModuleParachute)part.Modules["ModuleParachute"];

otherModule.bar = 10;

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