# Assigning a new parameter to an existing GUI slider?

**URL:** <https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740>\
**Category:** General JUCE discussion\
**Created:** [May 18, 2018, 11:54pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740 "2018-05-18T23:54:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [May 18, 2018, 11:54pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/1 "2018-05-18T23:54:35Z")

</div>

Hi, my plug-in needs to have different parameters associated with a set of GUI controls.  
So I need to reassign SliderAttachment’s to different parameters when I want to.  
Does anybody know how I do that? I can’t figure it out. Do I just delete them when I need a change? That seems wasteful, and unsafe.  
I currently keep all my sliders in a…

```auto
OwnedArray <AudioProcessorValueTreeState::SliderAttachment> sliderAttachments;

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 19, 2018, 9:52am UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/2 "2018-05-19T09:52:54Z")

</div>

Based on the API here: [https://docs.juce.com/master/classAudioProcessorValueTreeState.html](https://docs.juce.com/master/classAudioProcessorValueTreeState.html) you should probably not add/remove parameters on the fly.

Use some other logic to `setVisible(true/false)` your sliders as needed. I’m pretty sure that hidden GUI elements don’t receive mouse events so you don’t have to worry about if a slider that shouldn’t receive mouse clicks will receive those mouse clicks. but it’s super easy to test…

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [May 19, 2018, 10:22am UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/3 "2018-05-19T10:22:34Z")

</div>

I have 60 different selectable objects that will use the same 6 controls at different times. I’m not sure that creating all those gui elements is the best way of doing it - to make the unused ones invisible? Seems a little crazy to me, but maybe I’m old fashioned.  
Thanks for answering.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 19, 2018, 3:26pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/4 "2018-05-19T15:26:20Z")

</div>

IMHO both ways should work and I see no problem with either one. In terms of resources it will end up the same. A hidden Slider doesn’t eat CPU, except setting the variable “current value”.

I would base my decision on code simplicity:

- if the sliders appear to the user as generic controls, I would delete the attachments and create new ones, the GUI code is not affected
- if the GUI could be different panels depending what setup was chosen, I would write a separate GUI for each setup, with the appropriate descriptions for each settings, so I can switch the GUI with a single line and don’t have to worry, that the right description to the right attachment is displayed.

But it’s a many-ways-to-Rome case.

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [May 19, 2018, 3:51pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/5 "2018-05-19T15:51:23Z")

</div>

Thanks Dan. It’s a shame I can’t just reassign an existing attachment, that would be better- so I can run through them and say “you were looking at that parameter but now it’s this one.”

I’m going for the create everything and show what’s needed approach. I was a little worried about the time spent looking up parameter names. But it’ll be fine, I’m sure they’re all hashed anyway. 🙂

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 19, 2018, 4:08pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/6 "2018-05-19T16:08:35Z")

</div>

> [@DaveH](#):
>
> But it’ll be fine, I’m sure they’re all hashed anyway.

Not really, it works differently. The attachment is a listener to the component, so no lookup is needed. And on the other hand, they are parameterListeners on the AudioProcessorValueTreeState (and internally bound to a parameter in the tree, also no lookup needed).

But you can use that for your attachments, if you want easy access:

```auto
std::unordered_map<Component*, std::unique_ptr<AudioProcessorValueTreeState::SliderAttachment>> attachmentMap;

// link
attachmentMap [&slider] = std::make_unique<AudioProcessorValueTreeState::SliderAttachment> (state, "paramID", slider);

// unlink:
attachmentMap.erase (&slider);

```

Haven’t tested it, but should work…

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [May 19, 2018, 8:32pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/7 "2018-05-19T20:32:05Z")

</div>

I’m just getting " ‘unordered\_map’: is not a member of ‘std’ " which is odd.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 19, 2018, 9:58pm UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/8 "2018-05-19T21:58:17Z")

</div>

it needs an include, see [cpp-reference.com](http://en.cppreference.com/w/cpp/container/unordered_map)

`#include <unordered_map>`

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [May 20, 2018, 10:41am UTC](https://forum.juce.com/t/assigning-a-new-parameter-to-an-existing-gui-slider/27740/9 "2018-05-20T10:41:16Z")

</div>

Nice, thanks man. I’ll have to find out more about that…
