We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hallo, sorry for my bed English, hope you understand me anyway. How big is the systemvariable frameCount? How many frames can I count with it? Thank you!
Answers
It's an int so it should be 2^31 = 2,147,483,647
Thank you!!! is it possible to reset the variable without restarting the program?
just define your own global long and increment it in draw()
According to the javaDoc
frameCount
ispublic
so:At least worked on sketchpad.cc, BUT as it is js, perhaps won't work in java mode...
Thank you!!!
We can fearlessly reset it back to
1
. Resetting to0
is possibly problematic (or not). 8-XIf you're afraid frameCount might over carry beyond
2**31 - 1
, just check for it: :-SSif (frameCount == MAX_INT) frameCount = 0;
It works fine - thanks again!
void draw(){println(frameCount); frameCount = 0;} // prints 1 forever
Of course it prints 1 for ever, because it is being reset to zero very frame!
This works
after how many days would processing crash because of
frameCount
when we forgot to reset it...?24855.135 days or a little over 69 years ;))
I'll worry about this later then...
2,147,483,647 frames / 60fps =
35791394 seconds / 60 seconds in a minute =
596523 minutes / 60 minutes in an hour =
9942 hours / 24 hours in a day =
414 days
My mistake I forgot the 60fps :\">
so over a year....
thanks!
Of course... Was intended. :)
TL;DR: the frameCount on a 60fps Processing sketch keeps going forever BUT it re-runs
setup
every 2.3 years.Re:
frameCount
's range, and setting it, I wanted to contribute a few additions / corrections. I learned a few things when I took a look at the PApplet source:frameCount==0
runssetup()
frameCount!=0
runsdraw()
(as well as any librarypre()
andpost()
, mouse positions, events, etc.)frameCount
is incremented afterdraw()
setup()
So, a sketch running at ~60fps should run at least 828 days and probably indefinitely (although it may partially or completely restart).
setup()
is called on frame 0 (draw()
does not run)Depending on what code is present in the sketch
setup()
and how it reruns then the sketch may restart, or it may partially restart, or it may generate an error. However by default a sketch will just keep going indefinitely. Here is an example of a sketch going indefinitely, at high speed. It jumps forward in time in 100 day increments (always being sure to execute the 0 frame when required):We can see from the output how it is looping:
Watch what happens when we skip 414 days into the future to the moment of a sketch
frameCount
overflow -- in slow motion at 1 fps:...frameCount rolls over to negative and keeps incrementing up until (~414 more days later) it counts up to -5, -4, -3, -2, -1 , 0. When frameCount hits 0,
setup()
reruns.Here is an example that jumps to the moment before the setup re-run at frame 0. Note that because of its setup this particular sketch then jumps back to -5 and loops endlessly from -5 to -1:
Note in the above that frame 0 does not call draw, however it does take one full unit of time (one clock tick) to execute. As a result the "-1" sits on the screen for two beats. If Processing sketches did count on looping through frameCounts (whether at ridiculously high frameRate or for an extremely long run times) it would be easy to introduce an off-by-1 error in looping sketches by forgetting that draw is not called for one time unit each loop.
Playing with frameCount-based setup re-running can create some odd behaviors. Here is a short example: a sketch that reruns setup every frame, and uses setup to color-cycle the background. It is always on frame 1. Press any key to toggle off the reset behavior -- at which point frameCount advances normally in draw, however the background ceases to change because setup is no longer incrementing it.