We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I understand that if you want to be able to use variables globally you have to declare them at the top of the program. If you declare them inside a method they can only be used inside that method.
What I dont understand is when/where I should initialize variables, whether there are conventions regarding that. Should I initialize them in the first method that uses them, or in setup() or at the top?
Cheers,
Answers
For local variables - those defined inside a method - it's probably best to initialize them when you declare them. The one exception here is for arrays of values. If you're going to use a loop of some kind to initialize the values in the array, I would do the loop after all your local variables are declared.
For global variables, I would suggest you write your own reset() function to initialize them. Then call this reset() function from setup(). This way, if your sketch every gets to a point where it needs to "start over", you can just call reset(). This convenience outweighs all other benefits IMHO. Of course, if there are globals that WON'T need to get reset, then you can set their values in setup. Also, if you'e defining constants, well, those (should) have to be done when the variable is declared.
Summary:
Global variables that require values from the PApplet needs to be init in setup.
Kf
Those "problematic" PApplet's system variables are basically these 7:
100
. @-)