Jump to content

ConfigNode Issues with a couple of Vector Types


Recommended Posts

I've been fiddling around with the ConfigNode type a fair bit in the last few days and have struck an issue that I was wondering if anyone else has struck. Any ideas would be much appreciated.

I am doing this from code for a plugin and so using the ConfigNode.CreateConfigFromObject and ConfigNode.LoadObjectFromConfig to load/save an object and have the code structure worked out. I then tried to do this against all the types in the Parse functions - String, Bool, Enum, Vector2, Vector3, Vector3d, Vector4, etc. and I struck an issue with the Vector2 and vector3d types.

In a nutshell the short version of code looks like this and the String is successfully saved into the test.cfg file and loaded back from it - I can rem out the save line and edit the file and get the right value.

public class Storage
{
[Persistent] String TestString = "New String";
}
...
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class PersistingDataErrors: MonoBehaviour
{
Storage sTest = new Storage();
void Awake()
{
ConfigNode cnToSave = ConfigNode.CreateConfigFromObject(sTest, new ConfigNode());
cnToSave.Save(AssemblyPath + "/Test.cfg");

ConfigNode cnToLoad = ConfigNode.Load(AssemblyPath + "/Test.cfg");
ConfigNode.LoadObjectFromConfig(sTest, cnToLoad);

}
}

I can replace the String with a Vector3, Enum, Boolean and it all works as expected, but if I replace it With a Vector3d then on the save I get an Error "Cannot cast from source type to destination type.", but I can load a Vector3d.

If I try a Vector2, then the save part works, but on a load I get "Object type UnityEngine.Vector3 cannot be converted to target type: UnityEngine.Vector2"

I've tested using the Parse/Write functions for the two types and they work successfully to read write between a string and the specific object, but from the serialising functions I cant make these two work.

A full plugin source demonstrating it can be found here - https://ksppluginframework.codeplex.com/SourceControl/latest#Examples/PersistingDataErrors/PersistingDataErrors.cs

Any ideas or help would be ace.

Link to comment
Share on other sites

(don't really have anything to help but I have a question)

Since you're tinkering around with this and you definitely know a lot more about this than me but could you please explain how I could go about reading multiple nodes that are identical?

values

{

var_1 = 0;

var_2 = 20;

var_3 = 35;

}

values

{

var_s = 2;

}

values

{

var_2 = 21;

var_4 = 2;

}

Link to comment
Share on other sites

Trigger Au: I don't know about Vector2, but Vector3d is a KSP type, and it doesn't even have Parse methods.

theSpeare: you use ConfigNode []values = node.GetNodes ("values");

That will give you a potentially empty list of the matching nodes (ie, values{}).

Link to comment
Share on other sites

Hi taniwha, thanks for the response I did think that might be it originally, but looking at the ConfigNode Class in VS's Object Browser you can see these two methods so I think it should work:


public static Vector3d ParseVector3D(string vectorString)
public static string WriteVector(Vector3d vector)

I also used those to test the parse/writes to and from strings, it just seems to be the serialising function that fails, and only on one action, eg. It will load the Vector3d from a configNode serialised object, just wont write it.

theSpeare: taniwha's answer looks spot on to me, feel free to come back with more questions, although another thread might be a better place - or the developer question mega thread

Link to comment
Share on other sites

I've encountered something similar lately. The problem I believe comes from the fact Unity does not recognize the type of Vector3d. On my side I was trying to save an array of values as Vector3d in an OnLoad() override. When I set debug logs up, the array was full as expected during loading, but empty when it got to the VAB. The only thing I could do to get it to work was to switch to Vector3 and add a [serializeField] in front of my declared array.

As for parsing, I'd recommend using KSPUtil's ParseVector3d. I've been getting really weird results from ConfigNode's parsers lately, not sure what's up with them.

Link to comment
Share on other sites

Thanks Chris, unfortunately if I switch to the other methods I will have to write an object parser and reflect all the properties myself, which is beyond my knowledge.

If its an issue I might log it and use Vector3's for storing Vector3d and Vector2's in the meantime.

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