class methods
Class for creating custom events. Also used inside oF for its own events (see ofCoreEvents).
ie: To create a new event:
ofEvent<float> onVolumeChange;
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.
ie:
wrong:
vector< ofEvent<int> > events;
right:
vector< ofEvent<int>* > events;
newListener(...)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TFunction function, int priority)
newListener(...)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TObj *listener, TMethod method, int priority)
Last updated Tuesday, 19 November 2024 17:24:09 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
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