Monitor ofxGui ofxFloatSlider updates?


Currently, I have a float prev_slider_value that keeps track of when the value of a ofxFloatSlider gets changed. It gets set to the initial slider value in setup() and compared to the current slider value in update().

It works fine, but I was wondering if there was a better way to do this?

Hi, yes, you dont need to do that. you can either add a listener to your slider or use an ofParameter instead of the ofxFloatSlider, which is what I personally prefer.

Both have this function:

void addListener(ListenerClass * listener, ListenerMethod method)

so, if you have these declared in ofApp
you can make a function like the following

void onChanged(float& f){
// this runs when the slider or param changes

thus you can call during setup

//ofParameter<float> param;
param.addListener(this, &ofApp::onChange);
// or
//ofxFloatSlider slider;
slider.addListener(this, &ofApp::onChange);

Actually the whole purpose of ofParameter existence is that it triggers an event when its value changes, and you can use it not only for the gui, but for what ever else you might need.

1 Like

Thank you for your explanation!