Restart

edited January 2018 in How To...

What language statement will restart the program?

Answers

  • There is no

    Instead write your own function which resets all the variables like in setup and before setup

    Don’t call setup itself

    Chrisir

  • Except that of course fails to make all the resets that aren't in setup. Global variable initialisations.

    Thanks.

  • Yes, you need to reset the global variables as well (the stuff before setup)

    Basically make a function init that you call for reset and also from setup, so that there are no duplicate lines

  • edited January 2018

    If you want to programmatically reset your sketch, this does most of it:

      void setup{
        // all global variable values must be assigned in setup
        // and the canvas must be cleared
      }
      void reset(){
          frameCount = -1;
      }
    

    Setting frameCount to -1 resets the frameCount -- and when it increments into frame 0, it re-runs setup (without you trying to call it directly). This enables you to re-setup the sketch.

    If you also need a millis() runtime that is measured since the last reset, this gets a bit tougher, as millis() itself cannot be reset. However, you can make your own fairly easily.

      int millisLastSetup; // last setup time
      void setup{
        // all global variable values must be assigned in setup
        // and the canvas must be cleared
        millisLastSetup = millis();
      }
      void reset(){
          frameCount = -1;
      }
      void millis2(){
          return millis2() - millisLastSetup;
      }
    

    Now call millis2() to get the time since the last reset, millis() to get total time since launch.

    Conversely, if you need to know the total frames (not counting setup) since the sketch started -- (although you almost certainly don't) -- you can add a global variable for that and track it at the beginning of draw.

      int millisLastSetup; // last setup time
      int frameCountTotal;
      void setup{
        // all global variable values must be assigned in setup
        // and the canvas must be cleared
        millisLastSetup = millis();
      }
      void draw(){
        frameCountTotal++;
      }
      void reset(){
          frameCount = -1;
      }
      void millis2(){
          return millis() - millisLastSetup;
      }
    

    That gives you a basic sketch reset based on setup(), with features for accessing either millis and either frameCount. So, for example, you could have a sketch that you can reset as often as you want, but also does something after x minutes of execution time.

    Demo sketch:

    // 2018-01-15 Processing 3.3.6
    // forum.processing.org/two/discussion/25970/restart
    
    int millisLastSetup; // last setup time
    int frameCountTotal;
    int counter;
    
    void setup() {
      println("(re)starting " + millis());
      millisLastSetup = millis();
      counter = 0;
    }
    void draw() {
      frameCountTotal++;
      background(0);
      text(status(), 10, 12);
      counter = counter+1;
      if (mousePressed) {
        reset();
      }
    }
    String status() {
      return "CURRENT:\n  " +
        str(counter) + "\n  " +
        str(frameCount) + "\n  " +
        str(millis2()) + "\n" +
        "TOTAL:\n  " +
        str(frameCountTotal) + "\n  " +
        str(millis());
    }
    void reset() {
      frameCount = -1;
    }
    int millis2() {
      return millis() - millisLastSetup;
    }
    
  • Thanks. And then one would have to ecode the global variable initialisers.

  • Right -- you need to put any initializations in setup. Unless you don't -- notice that I intentionally did not initialize frameCountTotal in setup.

Sign In or Register to comment.