Jump to content

[API] Float doesn't work with KSP.IO.PluginConfiguration.GetValue


Recommended Posts

This is the method signature for KSP.IO.PluginConfiguration.GetValue:

[COLOR=#0000ff][B]public[/B][/COLOR] T GetValue<T>([COLOR=#ff0000]string[/COLOR] key, T _default);

When I do something like this:

configFile.[COLOR=#191970][B]GetValue[/B][/COLOR]([COLOR=#0000ff]"CUSTOM_1_VALUE"[/COLOR],   [COLOR=#00008b].25[/COLOR])

CUSTOM_1_VALUE with a value of .25 (as a double) is written to the config file. (We're assuming that the key CUSTOM_1_VALUE has not been defined in the config file, so GetValue will write the default value in for us.)

However, when I do this:

configFile.[COLOR=#191970][B]GetValue[/B][/COLOR]([COLOR=#0000ff]"CUSTOM_1_VALUE"[/COLOR],   [COLOR=#00008b].25[/COLOR]f)

CUSTOM_1_VALUE is not written at all to the config file, not even as a float.

Can somebody explain the reason behind this?

Edited by longbyte1
Link to comment
Share on other sites

Okay, I just checked and I think your problem is that your confignode is misconfigured.

public T GetValue<T>(string key, T _default);

means "in the confignode, get the value with the key name "key" with the key name being a string. If you can't get the value, use the "default" of type T instead.

So you run

configFile.GetValue("CUSTOM_1_VALUE",   .25)

and the call to the confignode fails for some reason, so the default of ".25" is used.

Typically, the commands used are:

configNode.SetValue("KeyName", Value); //Write a value to the config node at the specific key
Value = configNode.GetValue("KeyName")l //Get the value from a config file at the specified key and set the variable Value to what is pulled from the config file.

Hope that helps.

D.

Link to comment
Share on other sites

Okay, I just checked and I think your problem is that your confignode is misconfigured.

public T GetValue<T>(string key, T _default);

means "in the confignode, get the value with the key name "key" with the key name being a string. If you can't get the value, use the "default" of type T instead.

Actually, this does not seem to be the case. GetValue will actually create the key and set the default value for me if the key is not found. (I'd say this is an enhancement rather than an annoyance.) Take a look at the unofficial API doc yourself.

Edited by longbyte1
Link to comment
Share on other sites

I checked through the Object Browser in Visual Studio but did not actually test it so it probably behaves as you describe.

Certainly the ability to make the key if it does not exist is handy, but I can see where I would not want it. I use configNodes to load settings from disk a lot of the time, if I'm trying to load a setting that does not exist I want it to throw an error, not just silently create the key for me.

As always, more options is better. Go ahead and use what works for you.

D.

Link to comment
Share on other sites

So, I've tested it, and you're right.

Using this test plugin:


namespace Foo
{
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class Foo : MonoBehaviour
{
void Start()
{
var config = KSP.IO.PluginConfiguration.CreateForType<Foo>();
float v = 0.25f;
config.SetValue("CUSTOM_1_VALUE", v);
config.save();
}
}
}

And running, I got a shiny new config.xml file in the right place, with no data.

It looks like a bug to me.

Link to comment
Share on other sites

PluginConfigNode probably has some issue with objects. If you convert it to a string before passing it in, it'll work

config.SetValue("CUSTOM_1_VALUE",   v.ToString());

I'll also point out the KSPUtil class, which has a number of convenient static methods to help parse various KSP types to and from strings. It sometimes gets overlooked

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