We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › replay sketch on keypress
Page Index Toggle Pages: 1
replay sketch on keypress (Read 1428 times)
replay sketch on keypress
Apr 12th, 2010, 7:32pm
 
Greetings

I am in need of a method to replay a sketch from the beginning via a key press.

Any help will be GREATLY appreciated  Smiley
Re: replay sketch on keypress
Reply #1 - Apr 12th, 2010, 10:54pm
 
you could try a reset method, at least thats how ive always done it.

void reset(){

//reset all you variables here

}

then call reset(); when you want to restart the sketch.

However im sure there is a better way to do this.
Re: replay sketch on keypress
Reply #2 - Apr 12th, 2010, 11:03pm
 
Thanks for the reply.

I just worked out a way to do this which is similar. In void draw() I wrote if(keyPressed){//wrote initial variables here }

I'm sure there is a better way as well  Cheesy
Re: replay sketch on keypress
Reply #3 - Apr 13th, 2010, 1:08am
 
It's rather a matter of style than functionality, but as the function "void setup()" is the one run on start-up, it should (?) also be the one, which "resets" the sketch to the start-up condition.
A good way of "reset" in most of my sketches is the following function.

Quote:
void setup()
 {
   //do something
 }
void draw()
{
  // do something
}
void keyReleased()
{
 if (key==' ') setup();
}


Note that I use "keyReleased" rather than "keyPressed" as thus the sketch gets "reset" only once if somebody keeps the space-bar pressed. (Well, it gets reset once he releases the spacebar)
Re: replay sketch on keypress
Reply #4 - Apr 13th, 2010, 1:51am
 
I have a vague recollection of seeing PhiLho say you shouldn't call setup again from within draw(); but I don't remember the reason...  I suspect it's probably some Java thing that might only present a problem in certain circumstances; or my memory could be playing tricks on me.

Still the workaround is simple enough: you create a separate function - perhaps init() - that resets all your variables; call this from setup and then whenever you need to reset the sketch.

Hopefully someone will clarify whether I've got that right Wink
Re: replay sketch on keypress
Reply #5 - Apr 13th, 2010, 2:37am
 
Right. Perhaps it is harmless, but better safe than sorry, I would avoid calling size() again, as lot of internal stuff is done there...
Likewise, there are lot of settings not needing to be called again in general, like smooth(), frameRate(), re-loading data, etc.
Just make an initialization function resetting variables, re-generating arrays, etc.
Re: replay sketch on keypress
Reply #6 - Apr 13th, 2010, 3:13am
 
Ok, thanks for enlighting me. So the "clean" way of doing is to create a "void Init()" function which is also called from within the "void setup()" and then whenever needed, right?
Page Index Toggle Pages: 1