Right way to save settings of vector of ofParameter in a ofxguiGroup / ofxGuiGroupExtended

Hi everyone !

I’m using a vector of OfParameter as I only know in the setup function how many sliders, … I will have in my Gui.
It’s working pretty straight forward but when I try to save the settings of my vector of ofParameter only the last one is saved. And when I reload the seetings the value of this last one is set to all the ofParameter.

Is there a correct way to save the settings of the different ofParameter.?

Here is the code :
Header (You need to include ofxGui)

 //------- G U I -----------------
	

	ofxGuiGroup MaskWindow;
	ofParameter<int> family;
	vector<ofParameter<bool>>  Family_Blending;
	vector<ofParameter<int>>  FamilyAlpha_;

Cpp file

//--------------------------------------------------------------

void ofApp::setup(){
int _nFam = 3;

MaskWindow.setup("Mask Settings", "mask", 20, 20);
MaskWindow.add(new ofxIntSlider(family.set("Family", 0, 0, _nFam - 1)));
Family_Blending.resize(_nFam);
FamilyAlpha_.resize(_nFam);
for (int i = 0; i < _nFam; i++)
{
	string fam = "Family " + i;
	MaskWindow.add(new ofxToggle(Family_Blending[i].set("Blend", false)));
	MaskWindow.add(new ofxIntSlider(FamilyAlpha_[i].set("Alpha", 255, 0, 255)));
}	
}

//--------------------------------------------------------------
void ofApp::draw(){
MaskWindow.draw();
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 's') {
	MaskWindow.saveToFile("settings.xml");
}
if (key == 'l') {
	MaskWindow.loadFromFile("settings.xml");
}
}

Actually I was trying to do that with the ofxGuiExtended and ofxGuiPlus addons but it’s the same problem.
With this addons the code is
Header (you need to include ofxGui, ofxGuiExtended, ofxGuiPlus)

//------- G U I -----------------
	ofxTabbedPages menu;
	ofxGuiPage masques;

	ofxGuiGroupExtended MaskWindow;
	ofParameter<int> family;
	vector<ofParameter<bool>>  Family_Blending;
	vector<ofParameter<int>>  FamilyAlpha_;

And the cpp file

//--------------------------------------------------------------
void ofApp::setup(){
int _nFam = 3;

MaskWindow.setup("Mask Settings", "mask", 20, 20);
MaskWindow.add(new ofxGuiSpacer(10, 0, 0));
MaskWindow.add(new ofxIntSliderPlus(family.set("Family", 0, 0, _nFam - 1)));
Family_Blending.resize(_nFam);
FamilyAlpha_.resize(_nFam);
for (int i = 0; i < _nFam; i++)
{
	string fam = "Family " + i;
	MaskWindow.add(new ofxGuiSpacer(fam, 10, 0, 0));
	MaskWindow.add(new ofxToggle(Family_Blending[i].set("Blend", false)));
	MaskWindow.add(new ofxIntSlider(FamilyAlpha_[i].set("Alpha", 255, 0, 255)));
}

masques.setup("Masques");
masques.add(&MaskWindow);

menu.setup("My settings");
menu.setSize(500, 300);
menu.add(&masques);
}

//--------------------------------------------------------------
void ofApp::draw(){
menu.draw();
}

when you resize a vector like that it’ll create 1 element and then copy it to all positions. Since ofParameter has shallow copy semantics a copy of a parameter is realy a reference to it not a full copy so when you call set on the different positions of the vector you are changeing that 1 parameter you have created and for which there’s several references in the vector. to avoid it you can use push_back instead of resize but i would advice you to use ofParameterGroup instead and then just setup the gui group with it like:

//.h
ofxGuiGroup MaskWindow;
ofParameterGroup MaskParameters;

//.cpp
...
for (int i = 0; i < _nFam; i++)
{
    string fam = "Family " + i;
    ofParameter<bool> param("Blend", false);
    MaskParameters.add(p_blend);

    ofParameter<bool> param("Alpha", 255, 0, 255);
    MaskParameters.add(p_alpha);
}
MaskWindow.setup(MaskParameters);

you can also have separate parameter groups if you prefer to have them separated by type and then add the parameter groups to the ofxGuiGroup or directly to an ofxPanel…