0

I am trying to change layout in an Android app in Processing, layouts just being 2 different methods that are run inside void draw(), but the program seems to fail to recognize the change of the currentLayout value that I am trying to change in void OscEvent.

When I print currentLayout in OscEvent, the value changes and sets to 2 as it should, but if I print it in draw() it never changes. So this is what is happening:

  • I can print "change!"
  • the value of currentLayout is changed and noticed if I check it inside OscEvent()
  • the value of currentLayout is NOT changed nor noticed if I check it inside draw()
  • I am also able to change the value, and layout, successfully when i do it in methods that are not from the OscP5 library, I am not sure how that affects anything.

The important parts of the code:

public int currentLayout;

void setup() {
  currentLayout=1;//vilken scen man börjar på, kolla switchen i draw()
} 

void draw() {
  switch(currentLayout) {
  case 1:
    defaultLayout();
    break;
  case 2:
    task1();
    break;
  }
 println(currentLayout);
}
void oscEvent(OscMessage theOscMessage) {
  /* get and print the address pattern and the typetag of the received OscMessage */
 String[] list =split(theOscMessage.addrPattern(), '/');
  println(list[1]);
  if (list[1].equals("layout")) {
    println("change!");
    currentLayout=2;
  }
}

All help and suggestions are appreciated! Thanks.

refaul
  • 31
  • 1
  • 5
  • Are the osc functions threaded? You might be updating the variable on one thread, and not syncing it to the main UI? – Ancantus Feb 26 '13 at 19:39
  • Actually, I think they are! Any idea on how I should sync it to the main UI? Thanks for the answer. – refaul Feb 26 '13 at 20:00
  • I finally got it to work! With the answer of Ancantus I googled myself to this: http://stackoverflow.com/questions/2120248/how-to-synchronize-a-static-variable-among-threads-running-different-instances-o – refaul Feb 26 '13 at 20:52
  • See if that works, synchronized functions will lock a thread and let you make modifications, and hopefully update both threads on the other side. The other option would be to use a [Handler](http://developer.android.com/reference/android/os/Handler.html) and post messages to the UI thread. – Ancantus Feb 26 '13 at 20:58
  • Yes I made the OscEvent synchronized, so it works well now. Thanks a lot! – refaul Feb 26 '13 at 21:08

0 Answers0