problem with controlEvent() in controlp5

edited February 2014 in Library Questions

Here is my code:

void controlEvent(ControlEvent theEvent) {
     if (millis()>270) {
      if (theEvent.isGroup()) {
          label = theEvent.getGroup().getCaptionLabel().getText(); 
          if (FirstLoad) { 
                           b2(); 
                           FirstLoad = false;
          }
      }   
   }
}

I use if() here because it looks like this code runs when i just start my programm. Using this condition (millis()>270) i solved this problem, but i think it's not a good solution.

Comments

  • What exactly is the problem? Do you want to have an alternative for the millis() trick?

  • edited February 2014

    Yes, for some things, like setting values, events are sent when you create the controller. To solve this you can setBroadcast(false), do stuff, setBroadcast(true). Then you won't need your current workaround.

    Note that this is why it's always better to post a small working example of the problem, since your current code example misses the relevant code. Hopefully with these new insights you will be able to solve it.

  • edited March 2014

    Sorry guys, I was busy, yes i wanted to have an alternative, and thanks for the answer, but i didn't get it (because I don't actually know anything about Object-oriented programming). My code is quite big and I don't want to cut it down in order to find a problem. I just can describe it: there are some text fields, some buttons and a dropdownlist. When I click on an item of dropdownlist first time this code runs (b2() correspond to the button 2)

    void b2() {
      cp5.loadProperties((label + ".spin.ser"));
          clear1();
          counter = 0;
          strokeWeight(1);
          graph();
    }
    

    Becuase I use this condition:

    void controlEvent(ControlEvent theEvent) {
         if (millis()>1200) {
          if (theEvent.isGroup()) {
              label = theEvent.getGroup().getCaptionLabel().getText(); 
              if (FirstLoad) { b2(); FirstLoad = false;}
          }   
       }
    }
    

    I found out that without millis() trick, the first line runs (as though button is pushed just in the beginning), and others don't. Also, i started using fonts

    f = createFont("Arial",16,true);

    and i had to increase delay time to 1200ms (program loads longer with fonts).

    I think i'm gonna let it as it is, so thank you for answer anyway.

Sign In or Register to comment.