Dynamically assign a variable or object

Hi everyone,

I am brandnew to Juce and C++, but my first plugin is coming along with the help of all the topics here on the forum and things on Google.

What I have not figured out so far is how to do the following:

I have three faders: fader0, fader1, fader2

In my code I do a lot of formatting to the labels belonging to the faders etc, but I just down know how to address these faders in a loop.

So if I were to do:

i = 3;
fader+i.setValue = 12345;

in a loop that would be super helpful.

What do I need to look for online or can somebody help me please! :wink:

Many thanks!

Put your components into an array or vector. There’s however a twist that you have to then use pointers, with all the potential problems those can bring. Juce has a handy class OwnedArray that helps somewhat with that, though.

Something like :

// as a member variable of your editor class
OwnedArray<Slider> mySliders;

// in the constructor of your editor class
for (int i=0;i<numberOfSlidersWanted;++i)
{
  Slider* slider = new Slider;
  addAndMakeVisible(slider);
  // whatever else initialization you need for the slider...
  mySliders.add(slider);
}

// then to access the sliders elsewhere in the code with a loop
for (int i=0;i<mySliders.size();++i)
{
  mySliders[i]->setValue(foo);
}

Wow!

thanks for the speedy reply! Will to try to use that right away.

And If I were to generate labels I just use:

OwnedArray myLabels;

// in the constructor of your editor class
for (int i=0;i<numberOfLabelsWanted;++i)
{
Label* label = new Label;
addAndMakeVisible(label);
// whatever else initialization you need for the slider…
myLabels.add(label);
}

?

OwnedArray<Label> if you want to use Labels.

cool!

So I’m almost there I think. But I’m not seeing the sliders only after I resize. Is there a way I can trigger this so I can put all the bounds information is resize?

this is in my MainComponent.cpp:

/*

This file was auto-generated!

==============================================================================
*/

#include “MainComponent.h”

//==============================================================================
MainComponent::MainComponent()
{
setSize (600, 400);

// in the constructor of your editor class
for (int i=0;i<3;++i)
{
    Slider* slider = new Slider;
    addAndMakeVisible(slider);
    // whatever else initialization you need for the slider...
    mySliders.add(slider);
}

}

MainComponent::~MainComponent()
{
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{

}

void MainComponent::resized()
{

for (int i=0;i<mySliders.size();++i)
{
    mySliders[i]->setBounds(100, i*100, 100, 20);
}

}

and this in my MainComponent.h file:

#pragma once

#include “…/JuceLibraryCode/JuceHeader.h”

//==============================================================================

/*

This component lives inside our window, and this is where you should put all

your controls and content.

*/

class MainComponent : public Component

{

public :

//==============================================================================

MainComponent();

~MainComponent();

//==============================================================================

void paint (Graphics&) override ;

void resized() override ;

private :

//==============================================================================

// Your private member variables go here…

// as a member variable of your editor class

OwnedArray mySliders;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)

};

Am I missing something?

The sliders will only show up after I resize…

Do the setBounds in paint() - that’ll do the trick! - EDIT: Bad idea, shame on me!

1 Like

No, doing setBounds in paint() is not really a good idea…the resized() method is the more appropriate place to do it.

1 Like

Do your editor’s setSize call at the end of its constructor after you’ve created and added the child components, not at the beginning.

1 Like

Thanks!

Your answers mean a lot to me :wink:

Have a great weekend!

flagged for moderation lol

2 Likes