Class for creating custom events. Also used inside oF for its own events (see ofCoreEvents).
ie: To create a new event:
To notify an event of that type:
float value = 10.0f; ofNotifyEvent(onVolumeChange, value);
To add a listener to that event, if the event is for example in an object called mySoundObject, and you want to register ofApp as a listener for that event:
ofAddListener(mySoundObject.onVolumeChange, this, &ofApp::onVolumeChange);
where ofApp::onVolumeChange is a function with the following signature:
void onVolumeChange(float & volume);
See the advancedEventsExample to see a complete example of how to work with events in oF.
Be careful when using events, it's a powerful structure but can also lead to really confusing code as there's no direct calls to an object.
It's important to unregister events in the destruction of listeners, if not an object that has already been destroyed can get called and the application will crash.
ofRemoveListener(mySoundObject.onVolumeChange, this, &ofApp:onVolumeChange);
Events have a private copy constructor to avoid that a copy of an object with an event gets all the registered listeners of the original. To be able to create a copy of an object that contains an event, use a pointer to an event instead of a normal var. This also means that you cannot store events directly in a vector or any other collection, the solution is the same, just use a pointer to an event.
vector< ofEvent<int> > events;
vector< ofEvent<int>* > events;
void ofEvent::add(TFunction function, int priority)
void ofEvent::add(TObj *listener, TMethod method, int priority)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TFunction function, int priority)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TObj *listener, TMethod method, int priority)
bool ofEvent::notify(T ¶m)
bool ofEvent::notify(const void *sender, T ¶m)
void ofEvent::remove(TFunction function, int priority)
void ofEvent::remove(TObj *listener, TMethod method, int priority)
Last updated Wednesday, 08 March 2023 18:08:02 UTC - 7c9ee203c70f155d301b41aa9b34dcc286921a3c
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue