Updating void draw() while another function is in a loop

Hi guys,

I’m having a bit of trouble with something. I have a FOR loop that’s running inside of a function. The loop loads a few file names from a website, and then adds them to a list.

My goal is to have the program update the draw event while this loop is running, to create a ‘progress bar’ of sorts. Basically I need to set up some kind of asynchronous for loop/draw loop that will update while something else is going on.

Is there any way for me to tell my program to update the draw event in each step of the FOR loop?

P.S. When I say “draw event” I actually mean “void draw() {}”. I’ve tried using a custom function called “drawUpdate(),” but it yields the same result. I’ve also tried placing the ‘draw event’ and ‘drawUpdate’ ABOVE the function containing the loop, but as expected, that does not work.

1 Like

You can place that for loop in a separate function which is called by thread(): :thread:
Processing.org/reference/thread_.html

You can update a global variable w/ the current index the for loop is currently processing. :globe_with_meridians:

And then, the draw() function can use that global variable to determine how big to draw the progress bar. :bar_chart:

2 Likes

Expanding on that, here is a little example:

void setup(){
 size(800,800,P3D);
 frameRate(10);
 thread("loadFiles");
}

int loaded = 0;

void draw(){
  println(loaded);
}

public void loadFiles(){
  int totalFiles = 10;
  for(int i=0;i<totalFiles;i++){
    /*load files*/
    try{
    Thread.sleep(1000);
    }catch(Exception e){}
    loaded++;
  }
}
1 Like