JUCE way of communicating data between components?


#1

Hi again.

Quick question regarding the "JUCE" way of communicating data between components. Say I have an OptionsComponent that has a Low / High settings value I want to use in my TargetComponent which can change at any moment. Whats the best way of getting that settings value in TargetComponent?


#2

The JUCE way is using the Observer pattern, or "Listener" in juce parlance. For example, if you want a component to react to a Slider move, you derive it from Slider::Listener and override the sliderValueChanged() method. You get a pointer to the Slider and can ask it for its new value and react accordingly.

Have a look at this tutorial:  https://www.juce.com/doc/tutorial_listeners_and_broadcasters


#3

So to clarify. If the value in OptionsComponent changes, the best thing is to send a custom Message with the updated value, have TargetComponent as a listener and handle the message and store the value locally?


#4

Or just call a method.

You only need all this listener/broadcaster stuff if you have dynamic relationships with multiple listeners that come and go, or if you need asynchronous messaging. Otherwise, just use plain old functions.


#5

So send in a reference to the OptionsComponent to the TargetComponent to call? 


#6

Sure, if that makes sense in your app. Keep things simple!