Problems with basic events

Hi.
I think i have stupid problem… but i don’t find the solution. I’m trying ofAddListener, i write this code:

  
  
ofAddListener(ofEvent.mouseReleased, this, &testApp::myCustomMouseReleased);  
  

and i have this 2 error:

  • Cannot refer to class template ‘ofEvent’ without a template argument list
  • Missing template arguments before ‘.’ token
    What is the problem?

Hi, I think your code should be

  
ofAddListener(ofEvents.mouseReleased, this, &testApp::myCustomMouseReleased);    
  

ofEvent is the basic event class, while ofEvents is a collection of predefined events.

Yes, right!
Thanks for explanation of my stupid question. :slight_smile:

No worries… ofEvents also had me confused in the beginning!

Another question… i think i will have many questions, i’m studyng these slides http://www.roxlu.com/blog/entry/145/openframeworks-007-presentations/page/ and this is the unique place where i can find people and ask!

So… i create my event class, only with file header:

  
  
#include "ofMain.h"  
  
class myTestEvent  
{  
public:  
    myTestEvent(string someData):data(someData)  
    {  
    }  
    string data;    
};  
  

After in my “testApp.h” i write:

  
  
#include "myTestEvent.h"  
  
...  
  
void myCustomEventListener(myTestEvent& args);  
  

And in “testApp.cpp” in setup:

  
  
ofEvent<myTestEvent> myCustomEventDispatcher;  
ofAddListener(myCustomEventDispatcher, this, &testApp::myCustomEventListener);  
  

If it’s all right, now i have a listener. I also create a function “myCustomEventListener” with a simply output.
But now… how can i dispatch my event? I see ofNotifyEvent, but doesn’t work and i don’t know how to use it.

Hi

You have to instantiate an ofEvent with your event as the templated parameter and then fire this event.

I didn’t test this code, but it should go something like this:

  
//create event and listener  
ofEvent<myTestEvent> theEvent;  
ofAddListener(theEvent, this, &testApp::myCustomEventListener);    
  
//notify event  
static myTestEvent event("evt");  
ofNotifyEvent(theEvent, event);  

It works! Thanks again.
I don’t understand why you use static. I know what is “static” but why do you think is better to use here?

Anyway, i have found one new thing that i didn’t know before. If i write:

  
  
class myTestEvent  
{  
public:  
    myTestEvent(string someData, int number):cont(number), data(someData)  
    {  
        
    }  
      
    string data;    
    int cont;  
};  
  

the values of someData and number go direct into data and cont. This is new for me, you know the name of this technique?

You’re just setting the values of those variables in the constructor. Underdoeg is right about dispatching the events, you just need to use ofNotifyEvent(). There’s a more advanced example in the of download advancedEventsExample, that might be helpful.

Yes, i have seen the example but it’s look little different from the slides. Anyway, now i have understand some more, so i come back to try again new stuff.
Thanks!

The term you’re looking for above is “initialization list”.
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
static: probably for scoping reasons.
http://www.learncpp.com/cpp-tutorial/811-static-member-variables/