Reading and saving ofVec2f / ofVec4f from ofxXmlSettings

Hi,
Is there an easy way to get a vector value from XML file?

I would like to have:

declaration in ofApp.h
ofVec4f vec;

setup in ofApp.cpp
settings.loadFile(“settings.xml”);

settings.xml

<settings>
<something>116, 32, 767, 511</something>
</settings>

It works for int and float but not for vectors…
vec = settings.getValue(“settings:something”, ofVec4f(0,0,0,0));

If you use ofxGui, you can load and save to an XML file, I was using this
ofParameter<ofVec3f> lightPos for an ofVec3f, I assume it works with a ofVec2f too.

Thanks for your help. I don’t use ofxGui and I don’t need it in my case or maybe this is the part which I need?

I was thinking about reading this setting parameter as a string and splitting it to int/float parameters and building a vector by myself but maybe there is a better solution.

the easiest is to store it as a string and recover it from a string, most OF types implement the stream operator which means that you can use them with ofToString and ofFromString so you can do:

ofVec3 vec3;
string str = ofToString(vec3);
vec3 = ofFromString<ofVec3>(str);

Thank you @arturo. This is what I expected.

Here is a pseudo code for others:

settings.xml

< settings>
< something>116, 32, 767, 511< /something>
< /settings>

Solution with ofParameter and ofxGui:
ofApp.h

ofParameter vec4;
ofxPanel guiFake;
ofParameterGroup paramGp;

setup ofApp.cpp

paramGp.setName(“somethingSettings”);
paramGp.add(vec4.set(“something”, ofVec4f(0, 0, 0, 0)));
guiFake.setup(paramGp);
guiFake.loadFromFile(“settings.xml”);

access to vec4:

vec4.get().x
vec4.get().y
vec4.get().z
vec4.get().w

Solution with string from xml file:
ofApp.h

ofxXmlSettings settings;
ofVec4f vec4;
string vec4Str;

setup ofApp.cpp

settings.loadFile(“settings.xml”);
vec4Str = cardSettings.getValue(“somethingSettings:something”, “0,0,0,0”);
vec4 = ofFromString(vec4Str);

access to vec4:

vec4.x
vec4.y
vec4.z
vec4.w

1 Like