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 › update() function called before draw()
Page Index Toggle Pages: 1
update() function called before draw() (Read 1324 times)
update() function called before draw()
Oct 21st, 2009, 4:33am
 
Im fairly new to processing, but done some C++ and ALOT of actionscript before. Im used to have a update() function called before draw();

So if you need to do calculations, and stuf before rendering a frame. It's kinda handy to have a update() always called before draw()

That's the case in openFrameworks.
setup();
update();
draw();

Could this be implemented in processing also ? if there's a update() override call it's being used. if the user doesn't create a update there,s no need for it for him/her ,.. then the current flow of setup(); draw(); can persist.

Anyone that understand what and why I want this.

Cheers! /Martin

Re: update() function called before draw()
Reply #1 - Oct 21st, 2009, 6:43am
 
Code:
void setup()
{
// do setup stuff
}

void update()
{
// compute stuff
}

void draw()
{
update();
// draw stuff
}
Re: update() function called before draw()
Reply #2 - Oct 22nd, 2009, 8:27am
 
What PhiLho said.

Think inside the box (where the box is the draw() function).

-spxl Smiley
Re: update() function called before draw()
Reply #3 - Oct 22nd, 2009, 9:03am
 
I don't think that having an update method that is called from draw is what aermartin was thinking of since it will not reduce the amount of computation in the draw function.

In the following code the setup() method is used to register the pre() method.

This effect of this is that pre() is called before each call to draw() i.e.
pre / draw / pre / draw/ .....

To stop the call to pre() then use deregisterPre()

There are other methods like pre() more info at
http://dev.processing.org/libraries/basics.html

Code:

void setup(){
  // blah blah blah

  registerPre(this);
}

void pre(){
  // do all the state calculations here
}

void draw(){
  // draw the objects here
}


Hope this helps.  Smiley
Re: update() function called before draw()
Reply #4 - Oct 22nd, 2009, 1:06pm
 
Quark, these methods are convenient for libraries, as they allow them to hook in the core of Processing life cycle.

But it offers no paramount advantages in a regular sketch: actually, it is somehow worse (although probably not in a really measurable way) because the pre() method(s) is called with reflection instead of using a direct call like I shown. And they are called exactly before draw(), so no more, not less than my method: they won't reduce the amount of computation either.
Re: update() function called before draw()
Reply #5 - Oct 23rd, 2009, 8:53am
 
I made a mistake in my original post when I said
Quote:
To stop the call to pre() then use deregisterPre()

it should be unregisterPre()

PhiLo, I didn't mean my post to be a criticism of your solution I only meant to offer an alternative. For simple sketches I suspect, like you, that there is little difference between them in terms of efficiency. Smiley

I offered my solution because it offers a clear separation between updating the 'data' and drawing of that 'data', which is what I think aermartin is after.

Also aermartin said

Quote:
if the user doesn't create a update there,s no need for it for him/her ,.. then the current flow of setup(); draw(); can persist.


and this can be controled using registerPre()/unregisterPre()

My final thought:
Quote:
these methods are convenient for libraries, as they allow them to hook in the core of Processing life cycle.

True but then surely they are convenient for regular sketches as well?

Even though they are documented in the developers section of the Processing website they are not so complicated to use especially for more experienced programmers like aermartin

Smiley

Re: update() function called before draw()
Reply #6 - Oct 23rd, 2009, 12:12pm
 
Well, having not written a library (yet?), I kind of overlooked these functions, so your input was useful. I didn't mean to diminish your information, just to balance it with the consequences... Smiley
From an OO/design point of view, they are indeed interesting.
Page Index Toggle Pages: 1