Lenticular wrote on Aug 18th, 2009, 4:04pm:I read that article. It seems as though all variables must be declared at the top irregardless of whether they are used as global variables.
Not at all - you can declare a variable locally within a function, without declaring it at the top, meaning its value can then only be accessed from within the function. e.g. if you moved "int x = 0" into setup the value of x would only be accessible from within setup and you would get an error. By declaring it at the top you make it global and therefore accessible from within setup, draw etc...
Lenticular wrote on Aug 18th, 2009, 4:04pm:In my case, I have two local variables. X and spacing. The use of these two over rides the global X and spacing variables at the top.
So the only globals being called upon are y, len and endLegs.
No - the x and spacing variables are global. I'm not sure you've understood the distinction between:
int x = 0;
and
x=0;
The first (when you specify what type of variable it is)
declares the variable - where you put this is important as it defines the scope of the variable. The second simply assigns a value to the variable, can only be done within the scope of the variable and thus can't be done until the variable has been declared. So when you use that in draw you're simply changing the value, not declaring the variable.
To make matters a little more confusing you can declare variables with the same name in different scopes, so the value returned will obviously depend on the point at which you access that variable name:
Code:int x = 0;
void setup() {
size(200,200);
int x = 13;
// prints the local variable x
println("(setup) x = " + x);
}
void draw() {
// prints the global variable x
println("(draw) x = " + x);
noLoop();
}
That might either make things clearer or confuse you more. There are times when it's perfectly valid to do this, but you might want to avoid it until you've got the idea of scope clear in your head...