I did not understand how events work. Can anyone explain it to me?

I would like someone to tell me how the “events” on “openFrameworks” work.

I’ve read the documentation ofEvents (http://openframeworks.cc/documentation/events/), but I can not really understand how it works.

I tried to copy the following code (from http://openframeworks.cc/documentation/events/ofEventUtils/#!show_ofAddListener), but it does not work (return error).

For instance, borrowing from the examples/events/SimpleEventsExample, if we make a class that will broadcast an event:

class SimpleEventNotifier {
    ofEvent<float> notify;  // will send an event with a float

void sendEvent() {
    ofNotifyEvent(notify, ofGetElapsedTimef(), this); 
}

};

We can listen for that in our ofApp:

SimpleEventNotifier notifier;
void ofApp::setup() {
    ofAddListener(notifier.notify, this, &ofApp::haveBeenNotified);
}

void ofApp::haveBeenNotified(float &f){
    ofLog() << " event at " << f << endl;
}

:dizzy_face:

Events in OF are based on the Observer pattern https://en.wikipedia.org/wiki/Observer_pattern

The observer pattern, putted simple, it’s just composed by a listener that listen to certain actions, and some emitter that emit some actions. Once an emitter send an action, and this action is what the listener is listening for, than you can tell to the listener to do something.

Search for “simpleEventsExample” in the example folder. In the ofApp.cpp file there are these lines:

    //------------registering (adding) listeners ----------
    
    ofAddListener(redCircle.clickedInside, //the ofEvent that we want to listen to. In this case exclusively to the circleEvent of redCircle (red circle) object.
                  this, //pointer to the class that is going to be listening. it can be a pointer to any object. There's no need to declare the listeners within the class that's going to listen.
                  &ofApp::onMouseInCircle);//pointer to the method that's going to be called when a new event is broadcasted (callback method). The parameters of the event are passed to this method.
    ofAddListener(Circle::clickedInsideGlobal , this, &ofApp::onMouseInAnyCircle);//listening to this event will enable us to get events from any instance of the circle class as this event is static (shared by all instances of the same class).
    
    //Notice that when calling the static event we are using :: while when calling the instance event we use a . (dot).
  }
//--------------------------------------------------------------

Is this enough to clarify your doubts? if not, what specifically it is not clear to you?

Thanks Edapx.
That example I had seen before, but I was not clear about the “listener” / “event” interaction, especially at the syntactic level.
I make a simple example, just to understand: how can I replace “if loop” in the following code with a “listener” / “event”?

ofApp.cpp

void ofApp::setup(){

    pos.set(200,200)
}

void ofApp::update(){

    pos.y += 1; 
}

void ofApp::draw(){

    if(pos.y >= 500)
    {
       ofSetColor (ofRandom,ofRandom, ofRandom);
    }
    ofCircle(pos.x, pos.y, 50);

}

What error do you get?
if it was a compiling error, I guess it is from visibility of member notify.

Will it solve if inserting public:?

class SimpleEventNotifier {
public:
    ofEvent<float> notify;  // will send an event with a float

void sendEvent() {
    ofNotifyEvent(notify, ofGetElapsedTimef(), this); 
}

};

This should help