ofNotifyEvent not needed to trigger an event? (stupid question? Just want to understand how this confusing concept works)

In Lewis Lepton’s “openFrameworks tutorial - 021 ofxGui and sound”

LINK:

Lewis doesn’t use ofNotifyEvent and he sends the addListener method to the object he wants to listen to.

In another example posted on these forums:

The process seems different:

#1 ofEvent timeStampEvent instance defined

ofEvent timeStampEvent;

#2 ofNotifyEvent Notifies timeStampEvent

void testApp::update(){
if( ofGetMousePressed(0) ){
string str = ofGetTimestampString();
ofNotifyEvent(timeStampEvent, str, this);
}
} //This appears to be the beginning of the chain. ofNotifyEvent notifies timeStampEvent.

#3 A Listener Listening to timeStampEvent Triggers the eventsIn method

ofAddListener(timeStampEvent, this, &testApp::eventsIn);

#4 eventsIn method called

void testApp::eventsIn(string & eventStr){
cout << “we got an event!” << endl;
cout << “timestamp is:” <<endl;
cout << eventStr << endl;

displayString = "last timestamp received: \n" + eventStr;

}

In the openFrameworks documentation addListener is NOT sent to an object. In Lewis Lepton’s tutorial addListener IS sent to the object that it is supposed to listen to. Is this the new way of doing things? Can someone please explain how all this works? It’s very confusing.

I did not watch the entire video, but I think the misunderstanding comes from confusing the static ofAddListener() function with ofxButton::addListener(), which is a member function of the class ofxButton.

The easiest way to understand what’s going on is looking inside ofxButton’s source code: you’ll notice that the class makes normal use of ofAddListener() and ofNotifyEvent().
And, while solving your doubt, you’ll also learn how to make classes that register to OF’s main events :smile:

If anything is still unclear, just ask.

1 Like