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:
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:
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.
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;
ofEvent::ofEvent(const ofEvent< ArgumentsType > &mom)
ofEvent< ArgumentsType > & ofEvent::operator=(const ofEvent< ArgumentsType > &mom)
comments powered by Disqus
Friday, 29 August 2014 16:22:35 UTC