ControlP5 doesn't modify inherited variables of plugTo object

151 Views Asked by At

In the following example B class extends A class, it inherits int a variable.

the cp5 slider is plugged to an instance of a B object, and the assign variable is "a".

    import controlP5.*;
    ControlP5 cp5;
    B b;
    void setup()
    {
      size(200,200);
      b = new B();
      cp5 = new ControlP5(this);
      cp5.addSlider("a")
         .setPosition(20,20)
         .setRange(0,255)
         .plugTo(b,"a");
    }

    void draw()
    {
       println(b.a , frameCount);
    }

    class B extends A 
    {
      B()
      {
        super();

      }
    }
    class A 
    {
      int a;
      A()
      {
        a  = 0;
      }
    }

the a value printed in console is always 0, so the slider isn't modifying the a variable.

How can I make controlP5 work for the inherited variables of a class?

1

There are 1 best solutions below

0
On BEST ANSWER

The short story is that I don't think you're going to get it to work like this.

Looking at the ControlP5 source, it uses reflection under the hood when you call the plugTo() function.

Specifically, it uses the getDeclaredFields() function, which only returns the fields in the child class, not any parent classes.

The way I see it, you have three options:

Stop using inheritance. ControlP5 wasn't designed to work with inheritance like this. So just go with simple classes and you'll be fine.

Request this feature. You could file a feature request for this. But I'm guessing this wouldn't be a high priority, since it's not a typical use case.

Do it yourself. ControlP5 is open source. So you could just make the changes yourself. Or you could write a simple workaround, maybe a different class with a function that maps to the child class somehow.