Help with polymorphism? Calling functions in other classes

Hello! I am struggling a little bit with Polymorphism. Trying to switch over my mind from Java still.

Here is how my classes are currently set up. (Please forgive the crude diagram)

img1

I would love for there to be only one OSC Class. This class should only be setup once, since it is networking with other devices. In theory, I would love for my classes to look something like this.

img2

Where ofApp can call the setup function of the OSC class, but the subclasses can also call functions in OSC.

I have the openFrameworks ball example downloaded, and I think the solution may involve polymorphism - but I am unsure if that’s exactly what I need.

I have tried it so that the OSC class is in the GUI class, but that gives me issues with having the children talk back to the parent - which then talks to the OSC class.

Is there another method I could be using?

Thanks all

What i would is to have a instance of “your osc” in ofApp at the same level as gui, and in those gui elements put a callback that you listen on ofApp and inside those callbacks you call your osc… hope that makes sense

Thank you for the suggestion!

Just so I know I am on the right track, would that mean looking into “events”?

https://openframeworks.cc/documentation/events/

the gui component will fire a function when the value is changed, look in the example

1 Like

So I am looking into this solution, however, I am still unsure how it works with multiple sub classes… Would that involve adding a listener per parent class?

I realize this really is not the solution, but I really do wish there was a way to just extern an entire class.

Being able to call a function from anywhere is what I would love to do.

Is only my opinion but i have the feeling that you are complicating things, sometimes i better to start from the begining… what you want to do? the schema you posted works perfectly with the option i told you.

Oh I am 100% sure I am overcomplicating things.

I have an OSC class that holds all my setup and network functions. Currently (for example) my sliders all have different ID’s given to them in their setup. In the sliders.mouseMoved I have it calculating the output then sending a function to it’s child OSC class.

osc.sendShutter(ID, output)

I think I am having issues with understanding the events. Before, I had a similar solution without using listeners but was having issues of only wanting to call the osc function when that slider was moved. That is when I created multiple OSC sub-classes, but I am trying to get away from that now.

My apologies, I think I am close to understanding. I successfully recreated it in a sample file but am now having issues integrating it.

GUI class

ofAddListener(angleA.oscOutputEvent, this, &GUI::newOSC);

SLIDER class

ofEvent<float> oscOutputEvent;
    void oscOutput() {
        ofNotifyEvent(oscOutputEvent,anglePercent);
    }

Except when I do this, the GUI Class gives me the error:
“9. In instantiation of function template specialization ‘ofAddListener<ofEvent<float, std::__1::recursive_mutex>, std::__1::basic_string, GUI>’ requested here”

you have custom made gui? post the code in a gist or similar if you can, i see some errors in that lines but better to see the complete files

I mean yes, it started out as a GUI but then has morphed. Probably need to refactor soon. Unfortunately, im away from the computer for a while and the code is messy (as you can probably guess). What would you say needs looking at so I know what to include?

Never mind, it was a type mismatch.

Thank you so much for your help!

I guess my last question on this subject would be, is there a way to pass multiple values through the listener? For example, a string and a float?

I love using ofEventListener objects to do the listening. Since they’re objects, they’re destroyed when they go out of scope, and you don’t have to delete the listener like with ofAddListener. You can use lambda functions with ofEventListener, and I’m thinking that the lambda can capture multiple arguments to pass to its function. If you capture ofParamter types, they have to be const if I remember correctly.

There is an awesome blog post about ofEventListener here: https://blog.openframeworks.cc/post/173223240829/events.

1 Like