I’m enjoying learning about processing and I love the features of ControlP5. I’ve been using ControlP5 controls that link to global variables in my script. Then I tried to make a class to organize my variables a little more. Uh oh… maybe that was a mistake! I thought there would be some way to link a ControlP5 control to a public variable exposed by my class. An imaginary function like “link this control to this other thing.” But I can’t find anything like that. Is there any way to do that? Or am I stuck with a big list of globals at the top of my file if I want to link them to CP5 controls?
Here is an example:
import controlP5.*;
ControlP5 cp5;
boolean myBoolean;
myObject obj;
void setup() {
size(800,800);
cp5 = new ControlP5(this);
obj = new myObject();
cp5.addToggle("myBoolean");
cp5.addToggle(obj.myBooleanInsideObject); // any way to do something like this?
// (link control to variable inside object)
}
void draw() {
background(0);
}
public class myObject {
public boolean myBooleanInsideObject;
myObject () {
myBooleanInsideObject = true;
}
}
Thank you so much in advance for your generous help!
Crap, I thought I understood how to do this, but I don’t. It seems like the way plugTo is implemented I can’t have multiple instances of the same kind of object because ControlP5 can’t have multiple controls with the same name? e.g., if I want to use PlugTo to link two ControlP5 controls to:
obj1.theValue
obj2.theValue
How do I avoid naming both ControlP5 controls “theValue”? They are not allowed to have duplicate names.
import controlP5.*;
ControlP5 cp5;
boolean myBoolean;
myObject obj1, obj2;
void setup() {
size(800,800);
cp5 = new ControlP5(this);
obj1 = new myObject();
obj2 = new myObject();
myBoolean = true;
cp5.addToggle("myBoolean");
cp5.addToggle("myBooleanInsideObject")
.setValue(obj1.myBooleanInsideObject)
.plugTo(obj1);
cp5.addToggle("myBooleanInsideObject") // doesn't work
.setValue(obj2.myBooleanInsideObject)
.plugTo(obj2);
}
void draw() {
background(0);
text("myBoolean is " + myBoolean,100,100);
text("obj1.myBooleanInsideObject is " + obj1.myBooleanInsideObject,100,150);
text("obj2.myBooleanInsideObject is " + obj2.myBooleanInsideObject,100,200);
}
public class myObject {
public boolean myBooleanInsideObject;
myObject () {
myBooleanInsideObject = true;
}
}
Thanks so much! I understand now. I appreciate the help.
I’m sorry this is turning into something of a saga, but I have one other question about this. I expected plugTo would link the controller to a variable such that if I change the controller the variable changes, andviceversa. But it is only one-way, it changes the variable but does not reflect the variable’s state when it is drawn.
It looks like there used to be a controller.setAutoUpdate() method that would change this behavior and also have the control reflect the current state of the variable, but it is on the deprecated list. Does anyone know if this functionality exists elsewhere? I thought it might be related to setUpdate() / isUpdate but I can’t get that to do this.
Note: I realize I can use setValue() every time I change a variable programatically in order to update the linked controller, I’m just looking for an easy way around having to write that every time. I thought setAutoUpdate() was the answer, but nope.