Waiting for input via OSC to continue function

Hi,

what i’m trying to do is to wait for the feed back of a distant video player to continue some process on an other machine. This process is not in draw and is ran only once. Basically:

boolean end = false;
void setup(){
.....
}

void draw(){
....
}

synchronized void oscEvent(OscMessage theOscMessage){
//following is waiting for the feedback of the distant player
if(theOscMessage.checkAddrPattern("/end")==true){
end = true;
}

//following is where the process is triggered 
if(theOscMessage.checkAddrPattern("/player")==true){
myplayerfunction1();
while(!end){
println("wait...");
}
myplayerfunction2();
}
}

I understood why this is not working: the code get stucked into the while loop, and then can’t receive the info to change the state of “end” boolean…
I’ve been searching for this but can’t find info on the proper way to do it…

Does anybody have some tips?

Thanks!

1 Like

You MUST change the variable used to trigger while inside the while Loop itself. Just put the first if Statement inside the while Loop. That should do the trick.

@Lexyth yes I tried that, but since my while loop is already hapenning inside an OSC callback, if I put

if(theOscMessage.checkAddrPattern("/end")==true){
end = true;
}

It never gets called because it is combining 2 OSC addresses inside each other…
I’m trying to put this in every directions but I’m always ending nesting 2 functions or other impossible thing…
My guess is that i’m just not having the good method to do what I want to do, but can’t find a way to do it…

Well, the best thing i can suggest for now is to use the noLoop()/loop() function to activate and deactivate draw. If i got what you want to do, just call noLoop() right after end = true; and call the println(„…wait“) inside draw. And call loop() inside there where you got the while Loop right now.

@Lexyth thank you for that tip.
I guess I need to go through a fundamental restructure of my code to have controls in the draw() loop.

1 Like

Well, i don‘t know your exact code, But if it‘s only for those 2, then it Would suffice to do this :

boolean end = false; //not needed if you don‘t have anything Else in draw
void setup(){
  .....
}

void draw(){
  println(„wait...“);

// or if you have something Else in draw : 
  if (!end) {
    println(„wait...“);
  } 
}

synchronized void oscEvent(OscMessage theOscMessage){
//following is waiting for the feedback of the distant player
  if(theOscMessage.checkAddrPattern("/end")==true){
    noLoop(); // if you have something else in draw, then don‘t use this, But just use end = true;
  }

//following is where the process is triggered 
  if(theOscMessage.checkAddrPattern("/player")==true){
    myplayerfunction1();
    loop(); // if you have something Else kn draw, then don‘t use this, But end = false;
    myplayerfunction2();
  }
}

thanks @Lexyth
I’ve ended up moving some controls in draw().
Checking if myplayerfunction() is still playing or ended and a switch() inside the if() loop to move to the next function each time one has ended.
Works fine now, I just need to break down a bit more each step, but maybe it’s for the best

I guess the while() option was really not the good option from beginning for me!

thanks again

1 Like

Glad it works now :blush: it‘s always a good idea to break code down as much as possible :wink: