Trigger action when variable change value

Hello!
Im need to trigger action every time ofSeconds()
How can i check is the value has change from previos frame to the current?

hello,

you can use ofEvents. Create an event dans add a Listener to execute an function when the event occurs

Thanks!
Not sure how ofEvents works but need to be every second exactly of the time.
Can you give an example?

I am not sure you need ofEvents but you can find details here : http://openframeworks.cc/documentation/events/ofEventUtils.html#show_ofAddListener

use ofGetElapsedTimef() to get elapsed time and fire the event you created every second

yeah but how do you compare a variable from the current frame to the previous frame or do do i let the program know that the variable has changed?

in setup() grab the elapsed time

float time0 = ofGetElapsedTimef();

in update() check time and execute action every second :

float time = time0 - ofGetElapsedTimef();
if( time == 1) {
    // do action
}

Thats not working only gives negative numbers

my mistake. Try :

float time = ofGetElapsedTimef() - time0;
if( time == 1) {
    // do action
    time0 = ofGetElapsedTimef();
}

That doesn’t work too

ofApp.h

float time0;
int seconds;

ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){

    time0 = ofGetElapsedTimef();
    seconds = 0;
}

//--------------------------------------------------------------
void ofApp::update(){
    float time = ofGetElapsedTimef() - time0;
    if( time >= 1) {
        time0 = ofGetElapsedTimef();
        seconds++;
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackground(0);
    ofSetColor(255);
    ofDrawBitmapString(ofToString(seconds), 100, 100);

}
1 Like

Cool! thanks.
Thas good.

Just when it reach time 60 time start doing negative numbers!

Like 60 minutes or 60 seconds? it would probably be safer to use ofGetElapsedTimeMillis() since it is an unsigned variable and can never be negative, it just rolls over back to 0, still you will always have overflow in your variables when they reach the limit and overflow https://en.wikipedia.org/wiki/Integer_overflow which is hard to account for.

Another method would be to use ofGetSeconds() which uses the system time so you just have to check for any difference at all

if( time != ofGetSeconds()) {
    time = ofGetSeconds()
    // do action
}

it will be off the first time you need it to fire since you arent starting exactly on the second but after that it should be pretty close

1 Like

Yeah ! acutally im using ofGetseconds.

Cool-! thats more elegant.

Thats why you had negative numbers because ofGetSeconds goes from 0-59 so when time = 59 and ofGetSeconds returns 0 you have 0 - 59 = -59 @Gallo’s method would work until it overflows but just recognize what the function is returning when you use it

the ofxTiming addon might be of help

I also did a little addon:


It is probably not the most elegant code, but it does its job so far.