Running a sketch more than once without closing

edited March 2015 in How To...

Hi everyone,

My sketch gives me a .csv output file. I want to run my sketch multiple times within a loop (in every iteration I'm changing some parameters so that I will have different outputs) to get the output files for each run (I'm saving them with a different name at each time), but it always stops after the first time.

So how can I get my code to run more than once?

Answers

  • If you use saveTable() inside a for loop and make sure you assigned a different name, that should work. Share your code, so people could help you to find out what the problem is.

  • edited March 2015

    when you program in active mode (and not in static mode) draw() runs on and on

    unless you use noLoop somewhere

    see

    Static sketches and Interactive sketches

    http://portalsfordeveloping.wikia.com/wiki/Functions#Static_sketches_and_Interactive_sketches

    ;-)

  • Thanks for the replies. I think the code is really too long to share here. But I'm using Processing via Eclipse and I defined a main class in Eclipse that calls my main class in Processing. Here is the main method from my main class that I'm using to call Processing's main class, which is named as the Perry class:

    static public void main(String[] passedArgs) {

        for(int i=0; i<3; i++) {
            PApplet.main(new String[] {"ffg7.Perry" });
            coeffVariationUp[i] = (float) (1 - i*0.01);
            coeffVariationDown[i] = (float) (1 + i*0.01);
            aa = coeffVariationUp[i];
            bb = coeffVariationDown[i];
            mS = m + (i+1) + ".3dm";
            fS = f + (i+1) + ".txt";
    
            Perry Igniter = new Perry();
            System.out.print("i= "+ i);
            Igniter.setup();            
        }
    
    }
    

    I actually need two output files from each run, a text file and a .3dm (Rhinoceros) file. I can get all these successfully for the first iteration but then it just stops.

    • Your code deviates a lot from what's expected from a Processing's "sketch"!
    • Your main code should go into the class "sketch"'s setup() & draw()!
    • It's advisable to use Processing's own IDE (PDE) for small sketches!
  • It's not really a small sketch actually. The code has more than 1200 lines in 9 classes. That's the reason I switched to Eclipse from Processing IDE in the first place.

    In my code everything happens within the setup() method, I don't even have a draw() method. But it works just fine for the first time as I said on my previous post.

    I think all I need to do is to kill the process going on right after I get the desired outputs and right before the second iteration starts. So that the second iteration can start just as the first one. But I couldn't figure out how to do that.

  • edited March 2015

    Looking at your re-posted question here:
    http://forum.processing.org/two/discussion/9815/processing-at-eclipse-run-a-sketch-more-than-once

    I can foresee lotsa problems...

    • When we invoke PApplet.main(""), a new Thread called "Animation" is instantiated.
    • Along w/ an associated canvas or Surface.
    • Notice that your code inside your loop goes on while the newly instantiated PApplet runs!
    • It means that you'll probably end up w/ 3 PApplet instances running at the same time.
    • If those sketches happen to use some OpenGL renderer, either they will all crash or slow to a crawl!
    • It makes no sense directly invoking PApplet methods such as setup(), draw(), mousePressed(), etc.
  • Thank you for your replies GoToLoop. I instantiated PApplet inside the for loop because I want to get number of outputs that is the same with number of iterations. And for each iteration I need to run my code from scratch. So I think I should kill the newly instantiated "Animation" thread at the end of each iteration, so that it can be launched again at the next iteration. Does that sound right? And how can I kill the Animation thread?

  • Answer ✓

    Does that sound right?

    • Well, how would you know the sketch has already finished before killing it?
    • Can't you place the loop inside sketch's draw()?
    • Each draw() run would mean 1 loop iteration.
  • I did place the loop in the draw() method and it worked just fine :) thanks. The only problem I have right know is I can't get rid of the drawings from the previous iterations. I tried the background(int) function but had no luck. Do you have any idea how I can solve this problem?

  • edited March 2015 Answer ✓
    • I expressed somewhat wrongly! I meant that you could replace your loop w/ PApplet's draw().
    • That is, the draw() itself becomes the loop! It's called back by the "Animation" Thread automatically.
    • After 3 iterations, you call exit(). Just check variable frameRate at the end of draw().
    • Also, use variable frameRate in place of your previous iterator i.
    • And of course, only 1 instance of PApplet is enough! :-bd
  • Thank you again, I really appreciate the help.

    I followed your advise and everything looks great except one thing. I get one .3dm file output from each run of the iteration, actually this is the purpose of my code, but I can't get rid of the drawings from the previous iterations. So my final .3dm output file from the last iteration contains every drawing from the very first iteration to the very end. I have to find a way to prevent this. In the end I should have the output files with only the drawings from their own iteration.

    This means I have to find a way to clear everything in the end of an iteration before the next iteration of draw() method starts. The only function in the documents I found so far is to call background(int) function but it only changes the background and doesn't clear anything.

  • Well I solved that problem as well. I'm using a library (iGeo Computational Design Library) and have to call a function from there to clear the screen before each iteration. So it's all figured out. And again thank you for all your help.

Sign In or Register to comment.