Trouble getting access to AudioProcessor Variable in Component

I know, another noob who cannot do C++ :slight_smile:

I have a Component that sits on a Class that sits on a Tab that sits on the PluginEditor.

The Class that the Component sits on has access to the AudioProcessor variables via a reference passed in, Header File below:

class LibrarianPage  : public juce::Component,
                       public juce::Button::Listener,
                       public juce::Timer
    ~LibrarianPage() override;

    void paint (juce::Graphics&) override;
    void resized() override;

    KorgRadiasAudioProcessor& audioProcessor;

Then in the .cpp File:

#include <JuceHeader.h>
#include "LibrarianPage.h"
//#include "PluginEditor.h"

LibrarianPage::LibrarianPage(KorgRadiasAudioProcessor& p) : audioProcessor(p)
    // Librarian Components
    // --------------------

And I will admit that I do not fully know how this works but it does!

But the librarianComponent that gets constructed on the LibrarianPage (above) has no access to variables in the AudioProcessor, I tried to copy the way that the LibrarianPage does it but it won’t work!

The LibrarianComponent is all in one header file, like:

#pragma once

#include <JuceHeader.h>
//#include "PluginEditor.h"

class LibrarianComponent  : public juce::Component

    LibrarianComponent()//(KorgRadiasAudioProcessor& p) : audioProcessor(p)

    ~LibrarianComponent() override

	int startX;
	int startY;
	int dragX;
	int dragY;
	int endX;
	int endY;
	int mouse_down;
	int mouse_up;
	int mouse_drag;

	//KorgRadiasAudioProcessor& audioProcessor;

	void mouseDown(const juce::MouseEvent &event)
		startX = event.getMouseDownX();
		startY = event.getMouseDownY();

		//DBG("Mouse Down X " << startX);
		//DBG("Mouse Down Y " << startY);

		mouse_down = 1;
		mouse_up = 0;

	void mouseDrag(const juce::MouseEvent &event)
		mouse_drag = 1;
		mouse_up = 0;
		dragX = startX + event.getDistanceFromDragStartX();
		dragY = startY + event.getDistanceFromDragStartY();

	void mouseUp(const juce::MouseEvent& event)
		endX = startX + event.getDistanceFromDragStartX();
		endY= startY + event.getDistanceFromDragStartY();
		//DBG("Mouse Up X " << endX);
		//DBG("Mouse Up Y " << endY);
		mouse_down = 0;
		mouse_drag = 0;
		mouse_up = 1;

	void paint(juce::Graphics& g) override

		int comp_height = getHeight();
		int comp_width = getWidth();

		int margin = 10;

		int no_of_banks = 16;
		int no_of_patches = 16;

		int slot_width = comp_width / no_of_patches;
		int slot_height = comp_height / no_of_banks;

		int mem_width = (comp_width - (no_of_patches * margin)) / no_of_patches;
		int mem_height = (comp_height - (no_of_banks * margin)) / no_of_banks;

		// Draw the Background
		// -------------------
		juce::Rectangle <float> area = getLocalBounds().toFloat();
		g.fillRoundedRectangle(area, 8.0f);

		// Draw the Memory Slots & Patch Names
		// -----------------------------------
		for (int i = 0; i <= (no_of_patches - 1); i++)
			for (int j = 0; j <= (no_of_banks - 1); j++)
				juce::Rectangle <float> rect1(margin + ((mem_width * j) + (margin * j)), margin + ((mem_height * i) + (margin * i)), mem_width, mem_height);
				g.fillRoundedRectangle(rect1, 4.0f);

				g.drawText(audioProcessor.librarianPatchNames_SA[1 + i + (j * 16)], margin + ((mem_width * j) + (margin * j)), margin + ((mem_height * i) + (margin * i)), mem_width, mem_height, juce::Justification::centred, true);


The reference to audioProcessor.librarianPatchNames_SA (just above) which is declared in the PluginProcessor is what I need to get access to!

You can see the commented out bits where I tried the same approach to try and pass a reference to the AudioProcessor in but just got ‘no default constructor exists’ type errors!

I know I’m doing something dumb, but could someone be so kind as to explain my error please?

If you uncomment the lines in your LibrarianComponent header, the ‘no default constructor’ error can be solved by explicitly initialising the librarianComponent on your LibrarianPage constructor:

LibrarianPage::LibrarianPage(KorgRadiasAudioProcessor& p) : audioProcessor(p), librarianComponent(p)

Does that help?

willcassidy7 you’re my hero :slight_smile:

I’ll admit my C++ skills need some polishing up, what exactly is happening here?

We are passing a reference to the AudioProcessor into the LibrarianPage Constructor and calling it p ?

We then give p the name audioProcessor which we then pass into the LibrarianComponent as well?

Most of the problems I’m having are coming from that fact that I need Global Variables (for want of a better phrase) variables that I can access from just about anywhere because they hold global things like a String Array of Patch Names for instance!

At the moment I declare them in the PluginProcessor and then access them in the other Classes using that audioProcessor reference.

When it doesn’t work I get a bit lost, it was never this hard in LUA Scripting :slight_smile: