I can't call a function in a class from within a mouse event in another class

Is this supposed to not work, or am I missing something really trivial?
This is my code:

In main.cpp:

void ofApp::mouseDragged(int x, int y, int button){
    kinematics.mouseDragged(x, y, button);    
}

in kinematics.cpp:

 void kinematics::mouseDragged(int x, int y, int button){
     targetx = x;
     targety = y;
     communication.sendData(draai1, draai2, draai3);
 }

in communication.cpp:

void communication::sendData(int a, int b, int c){

    ofDrawBitmapString(a, 20, 150);
    ofDrawBitmapString(b, 20, 200);
    ofDrawBitmapString(c, 20, 250);
    device.writeByte(a);

}

I don’t get any errors, it just doesn’t execute the function in communication.cpp

communication.hpp:

 #ifndef communication_hpp
 #define communication_hpp

#include <stdio.h>
#include <ofMain.h>
#include "ofxIO.h"
#include "ofxSerial.h"

class communication{
public:
    void setup();
    void update();
    void draw();
    void sendData(int a, int b, int c);

   ofx::IO::SerialDevice device;
};

#endif /* communication_hpp */

the function gets executed but you can only draw from the draw function so anything you draw in the mouse callbacks for example won’t be seen in the screen.

Thanks, that makes sense :slight_smile:, but then i do have another question if you could be so kind to help me. It’s probably another really simple thing. Still learning C++.

Why can’t I edit the values that are used in the draw function from the other function? Is this because you can’t edit a memory location while reading it? The device.writeByte() function also doesn’t work in the sendData() function (which is called by the mouse drag), but it does work in the draw() function

void communication::draw(){
    ofDrawBitmapStringHighlight("Connected to " + device.getPortName(), ofVec2f(20, 20));
    ofDrawBitmapStringHighlight("See Console.", ofVec2f(20, 45));
    ofDrawBitmapString(motor1, 20, 150);
    ofDrawBitmapString(motor2, 20, 200);
    ofDrawBitmapString(motor3, 20, 250);
}

void communication::sendData(int a, int b, int c){
    motor1 = a;
    motor2 = b;
    motor3 = c;
    device.writeByte(a);
    ofLogNotice(ofToString(motor1));
}

that should work fine where are those variables declared?

They are called in that files header file, communication.hpp.
But:
The draw() function is called from the draw in ofApp.cpp and the sendData() function is called from another file, kinematics.cpp.

Thanks so much for your help

that shouldn’t be a problem. could it be that you are creating 2 different instances of the class? so you are setting the values in one and drawing the other?

or, how are you passing the instance from wherever you are creating it, i guess ofApp, to the class that calls sendData?

Thanks so much,
I was indeed creating 2 different instances, 1 in the main app files and 1 in another class. It all makes sense, and it works!