Readout and reuse the value of a gui-slider in setup()

hi

is it possible, to readout the value of an ofxIntSlider and use its value in the setup()?

(in following case particleNum)

void ofApp::setup() {
gui.setup();
gui.add(particleNum.setup(“particlesNum”, 200, 1, 2000));
gui.add(particleVal);

particles_vec.resize(particlesNum);

for (int i = 0; i < particlesNum; i++) {
	this->particles_vec[i] = Particle();
}

Hi @hp3007 , your code seems like it should work just fine if you change particlesNum to particleNum in the following lines:

particles_vec.resize(particleNum); //  change to particleNum here
// and also in the loop
for (int i = 0; i < particleNum; i++) {
	this->particles_vec[i] = Particle();
}

One thing you could do is to use an ofParameter instead of an ofxIntSlider. The ofParameter class is templated:

// in ofApp.h
    ofxPanel gui;
    ofParameter<int> particleNum;
    std::vector<glm::vec3> particles;

// in ofApp.setup()
    gui.add(particleNum.setup("particleNum", 200, 1, 2000));
    particles.resize(particleNum);
    ofLogNotice() << particles.size();

hi. thanks for your input.

sorry - that was a typing issue - i of course have particleNum in my code and basically it works. but i only get the value 200 in that case. so it gives me the initialized value at that stage. i hoped, there was a way to read out the setting of the slider in the setup-class. but i think, i have to remove the generation of the basic particle vector to the update-class. cause i can’t find a way to read out the data of the slider or also a ofparameter in the first class …

Hey yeah ofApp::setup() just runs once, at the very beginning, well before the gui is drawn for the first time. ofApp::update() sounds like a good place to get new values from the slider and use them as needed.

You could also use a listener (ofEventListener of ofAddListener()). The listener will run a function (like resizing a vector) when the value of an ofParameter changes.