Newb - if I declare a variable in PluginEditor.h , whats the right way to access it in PluginProcessor.cpp?

I have this which I declared in PluginEditor.h, mIansTextEditor
image

Its a textbox. In PluginEditor.cpp I draw it on the screen, set its size etc.
But in PluginProcessor.cpp , prepareToPlay, I want to write some text in there. But PluginProcessor.cpp does not have access to mIansTextEditor.
Whats the best way to make it available in PluginProcessor.cpp ?

You must take the problem the opposite way. The processor should never try to access anything that belongs to the editor, for the simple reason that the editor might not be there at all. When you close your plugin window, the editor instance is destroyed.

But you can create a member in your processor that your editor can access. In prepareToPlay you set this value in the processor, then in the editor paint method you can read the content of this variable and set the text box content. Your editor has a reference to the processor so you can easily do that :slight_smile:

If you often need to update this value, you can add a Timer in your editor to check the processor value regularly and update the text box

Thanks, thats better. However, only one error remains in PluginEditor.cpp:


The resized() function does not take in the &p that references the processor.
This function in PluginEditor.cpp does so it works there.

resized() does not. Any suggestions? Should I make resized() take in &p?

It is extremely suspicious that your processor owns a gui component like a “text editor.”

the member name in the editor is audioProcessor. p is the parameter passed in the constructor as an argument.

Ok, thanks, this works now:


I am inserting a textbox just to get to know that class and other widgets.

No you should not do it like that. The mIansTextEditor component should belong to the editor, not the processor. The processor should just have a member containing the actual text. Then the editor will fetch the text from the processor and use it to set the mIansTextEditor content.

thanks, but what if I am writing to the text box in the prepareToPlay() function which is in the PluginProcessor.cpp? I need to have access to the component at that point.

In your processor

public:
   juce::String& getMyText() { return myText; }
  void prepareToPlay() { myText = "myValue"; }
private:
  juce::String myText;

In your editor

void paint(juce::Graphics& g) { mIansTextEditor.setText(audioProcessor.getMyText());

Your processor does not need to know what the editor is doing. If your plugin window is closed when prepareToPlay is called, there will be no editor instance at all. It’s the editor’s job to fetch the information from the processor. If the processor must often change this information you can use a juce::Timer in your editor to fetch it and repaint. But there’s no point wanting to update the textbox exactly when prepareToPlay is called, processor and editor run in different threads.