Passing multiple arguments to ofEvent

#1

I’d like to pass multiple arguments (in this case a position, radius, and color) when calling an ofEvent. It seems ofEvent doesn’t support multiple arguments so I’ve tried wrapping them in a struct and just passing the struct, but I’m not sure I’m doing that right. I get this error when trying to do so:

From the output:

…\of_v0.10.1_vs2017_release\libs\openFrameworks\events\ofEvent.h(535,1): error C2661: ‘ofEventMyClass::args_drawCircle,std::recursive_mutex::make_function’: no overloaded function takes 3 arguments (compiling source file src\ofApp.cpp)

and

…\of_v0.10.1_vs2017_release\libs\openFrameworks\events\ofEvent.h(535,3): error C2672: ‘ofEventMyClass::args_drawCircle,std::recursive_mutex::addNoToken’: no matching overloaded function found (compiling source file src\ofApp.cpp)

I’m confused as to what I’m doing wrong as it says I have the wrong number of arguments but am using the same number of args (1 struct obj) as one would with an ofEvent that just passes an int or bool. What’s the proper way to pass a whole bunch of args via ofEvents?

Here’s the MVCE:

ofApp.h

#pragma once

#include "ofMain.h"
#include "MyClass.h"

class ofApp : public ofBaseApp
{

public:
    void setup();
    void update();
    void draw();
    void mousePressed(int x, int y, int button);

    struct args_drawCircle {
        int x;
        int y;
        ofColor col;
        float rad;
    };

    void drawCircle(args_drawCircle& args);

    MyClass myClass;
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup()
{
    // This seems to be the culprit- if you comment out this line build compiles successfully:
    ofAddListener(myClass.calldrawCircleEvent, this, &ofApp::drawCircle);
}

//--------------------------------------------------------------
void ofApp::update() {}

//--------------------------------------------------------------
void ofApp::draw() {}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{
    myClass.mousePressed();
}

//--------------------------------------------------------------
void ofApp::drawCircle(args_drawCircle& args)
{
    ofLogNotice("ofApp::drawCircle") << endl
                                     << "x: " << args.x << endl
                                     << "y: " << args.y << endl
                                     << "rad: " << args.rad << endl
                                     << "col: " << args.col << endl;
    ofSetColor(args.col);
    ofDrawCircle(args.x, args.y, args.rad);
}

MyClass.h

#pragma once
#include "ofMain.h"

class MyClass
{
public:
    void setup();
    void update();
    void draw();
    void mousePressed();

    struct args_drawCircle {
        int x;
        int y;
        ofColor col;
        float rad;
    };
    ofEvent<args_drawCircle> calldrawCircleEvent;
};

MyClass.cpp

#include "MyClass.h"

void MyClass::setup() {}

void MyClass::update() {}

void MyClass::draw() {}

void MyClass::mousePressed()
{

    args_drawCircle args;
    args.x = ofGetMouseX();
    args.y = ofGetMouseY();
    args.col = ofColor(ofRandom(0,255));
    args.rad = ofRandom(10,50);

    ofNotifyEvent(calldrawCircleEvent, args, this);
}
#2

Hi @a__, I think that I had the same poroblem as you in the past.

I think you have defined the structure of args_drawCircle twice, which creates the problem…

Try writing one definition in the myClass ( outside of it), then calling it in the ofApp.h.

I solved it differently though, by using an

ofEvent < vector < float > > callDrawCircle;

This way you won’t have any problems with matching the right amount of data in the struct…

Also you have defined the args_drawCircle

so you would get something like this in the class :

void mousePressed(){
        
        vector < float > circleEvent;
        
        circleEvent.push_back((float)( ofGetMouseX() ));
        circleEvent.push_back((float)( ofGetMouseY() ));
        // black and whiote with alpha
        circleEvent.push_back( ofRandom(255) );
        circleEvent.push_back( ofRandom(255) );
        
        circleEvent.push_back( ofRandom(10, 50) );
        
        
        ofNotifyEvent(calldrawCircleEvent, circleEvent, this);
    }

and then in ofApp.h :

    void drawCircle(vector < float > & args);

and


//--------------------------------------------------------------
void ofApp::drawCircle(vector < float > & args)
{
    ofLogNotice("ofApp::drawCircle") << endl
    << "x: " << args[0] << endl
    << "y: " << args[1] << endl
    << "rad: " << args[4] << endl
    << "col: " << args[2] << endl;
    ofSetColor(args[2]);
    ofDrawCircle(args[0],args[1], args[4]);
}

also as the function isn’t called in the draw function by through a call back, you might want to push a new item at the end of an array of circles, rather than drawing it, because nothing will show on screen.

Hope this helps,

++

P

#3

Thank you @pierre_tardif00, defining args_drawCircle statically in MyClass.h works.