# Why is .plugTo() not setting the initial value with ControlP5?

**URL:** <https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453>\
**Category:** Libraries\
**Created:** [September 11, 2018, 8:53am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453 "2018-09-11T08:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [September 11, 2018, 8:53am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/1 "2018-09-11T08:53:39Z")

</div>

Hi!

I’m using the controlP5 library to change variables inside an object. I use `.plugTo()` to specify which object contains the variables to control.

The issue is that the slider always shows 0 when the program starts, even if `cfg.opacity` is not 0. Calling `.setValue(160)` or `.update()` makes no difference, it still shows 0 when the program starts.

```auto
import controlP5.*;

ControlP5 cp5;
Config cfg;

class Config {
  int opacity = 160;
}

void setup() {
  size(200, 100);
  cfg = new Config();
  cp5 = new ControlP5(this);
  println(cfg.opacity); // prints 160
  cp5.addSlider("opacity")
    .plugTo(cfg)
    .setRange(0, 255)
    //.setValue(cfg.opacity) // does not help
    //.update() // does not help
    ;
}
void draw() {
}
void mouseReleased() {
  println(cfg.opacity);
}

```

I know the value IS connected to the object because if I change the slider and I quit the app, the right value gets saved to disk. It’s just that when I start it again it shows 0.

Any hacks around this?

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [September 11, 2018, 9:08am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/2 "2018-09-11T09:08:10Z")

</div>

Very weird. I found a workaround:

```auto
  float v = cfg.opacity;
  s = cp5.addSlider("opacity")
    .plugTo(cfg)
    .setRange(0, 255)
    .setValue(v)
    ;

```

So if I store the object property in a temporary variable it does work… ???

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 11, 2018, 9:09am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/3 "2018-09-11T09:09:35Z")

</div>

I think ControlP5 only mutates fields by reflection. 🤔  
But it never does the opposite. That is, it never reads fields. 😞  
Therefore, if we manually change a “plugged” field, ControlP5 will never realize it. 😳

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [September 11, 2018, 9:13am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/4 "2018-09-11T09:13:09Z")

</div>

Good to know, thank you 🙂 But why does it not work to `.setValue(cfg.opacity)`? There I’m directly specifying the value but it sees that value as 0. If I set it to `.setValue(cfg.opacity + 5)` the slider shows 5. Very odd.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 11, 2018, 9:23am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/5 "2018-09-11T09:23:36Z")

</div>

> [@hamoid](#):
>
> But why does it not work to `.setValue(cfg.opacity)`?

B/c apparently **plugTo()** mutates the plugged field right way: 😮

```auto
  println(cfg.opacity); // prints 160

  cp5.addSlider("opacity")
    .plugTo(cfg)
    .setRange(0, 255)
    .setValue(cfg.opacity)
    ;

  println(cfg.opacity); // prints 0

```

As a workaround, invoke **plugTo()** after **setValue()**: 😸

```auto
  println(cfg.opacity); // prints 160

  cp5.addSlider("opacity")
    .setRange(0, 255)
    .setValue(cfg.opacity)
    .plugTo(cfg)
    ;

  println(cfg.opacity); // prints 160

```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [September 11, 2018, 9:26am UTC](https://discourse.processing.org/t/why-is-plugto-not-setting-the-initial-value-with-controlp5/3453/6 "2018-09-11T09:26:23Z")

</div>

Ah I used to know this! Thanks for find it 🙂

So intuitive! 🤪

I opened an [issue in GitHub](https://github.com/sojamo/controlp5/issues/136),
