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 › Recursive calls to draw() function
Page Index Toggle Pages: 1
Recursive calls to draw() function (Read 500 times)
Recursive calls to draw() function
Apr 9th, 2008, 6:26pm
 
Am trying to implement a timer which only allows the draw method in my main class to run for a minute however when I put   a while loop around the call to draw() nothing happens however  it works fine when outside the loop any ideas??
Re: Recursive calls to draw() function
Reply #1 - Apr 9th, 2008, 6:47pm
 
Quote:


int stopTime;

void setup(){
 stopTime = 60000;
}

void draw(){
 if (millis() > stopTime){
   noLoop();
 }
 else{
   //do something
 }  
}


Re: Recursive calls to draw() function
Reply #2 - Apr 9th, 2008, 7:20pm
 
Hi thanks for the answer much appreciated however the problem is not solved by this. I'll try and explain it a bit better. I am trying to evolve some robots. Firstly I initiliase a population of robots. Then train them for a number of epochs using evolutionary approach as follows:

Firstly select 2 random members of the population.
Allow them in turn to explore the environment for 1 minute.
Using a fitness function rank the robots and evolve weaker one.

The problem is in my explore method I need to allow the robots to only explore for one minute. As you can see as the number of training epochs are very high I cannot statically define the time and use millis().

Pseudo-code of the problem below. noLoop() is active:


void draw() {
       
 image( ground, 0, 0);
 currentMap.draw();
       
 for(int i =0; i<epochs; i++){
   getContestants();
   explore();
 }


void explore(){  //This method works
 
 currentRobot = robots[robot2];

 currentRobot.move();
 currentRobot.draw();
 
 }

void explore(){ //This doesn't
 
 currentRobot = robots[robot2];

 while(timer > 1){
   currentRobot.move();
   currentRobot.draw();
}
}

Re: Recursive calls to draw() function
Reply #3 - Apr 11th, 2008, 5:15pm
 
You can still use the solution posted earlier ...

You just need to update the "stopTime" each time
you start one of your robot simulations.

Something like this:
  stopTime = millis() + duration;

-- djones
Re: Recursive calls to draw() function
Reply #4 - Apr 11th, 2008, 6:16pm
 
Hey thanks yer have been fiddling around and now have the timer working. Still don't understand why a draw call won't work from within a while loop but blah nvm!!!
Re: Recursive calls to draw() function
Reply #5 - Apr 11th, 2008, 7:17pm
 
It won't work because the screen doesn't get updated until the original draw function has finished.

Behind the scenes Processing does (in pseudo code):
Code:
 pre();
draw();
post();
updateScreen();


So even if you call draw from within draw, it still won't update the screen until that original draw has finished.
Page Index Toggle Pages: 1