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.
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
If you want to programmatically reset your sketch, this does most of it:
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, asmillis()
itself cannot be reset. However, you can make your own fairly easily.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.
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:
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.