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 › Using width and height
Page Index Toggle Pages: 1
Using width and height (Read 1072 times)
Using width and height
May 10th, 2010, 12:52pm
 
Can width and height be used outside of either setup() or draw()? I am trying to initialize some variables using width and height:
Code:
void setup()
{
 size(640, 480);
 background(#ddddcc);
 smooth();
}

float x_0 = width / 2;
float y_0 = height / 2;


But the values of x_0 and y_0 end up initialized as zero in draw(). I can post more code if needed but hopefully it is something simple I am over looking.
Re: Using width and height
Reply #1 - May 10th, 2010, 1:06pm
 
maybe like this?

Code:
float x_0, y_0;

void setup(){
 size(640, 480);
 background(#ddddcc);
 smooth();
 x_0 = width / 2;
 y_0 = height / 2;
 println(x_0); // proof
 println(y_0); // it works :)
}

void draw() {
}
Re: Using width and height
Reply #2 - May 10th, 2010, 1:13pm
 
Awesome! Thanks, amnon -- works perfectly. I had tried this before: Code:
void setup(){
 size(640, 480);
 background(#ddddcc);
 smooth();
 float x_0 = width / 2;
 float y_0 = height / 2;
without any luck, of course. If you, or anyone else, would indulge me: what is the difference when you call float var within setup() as opposed to declaring it outside then initializing it in setup?
Re: Using width and height
Reply #3 - May 10th, 2010, 1:34pm
 
The difference is the order in which the code is run, which is:

1. initialising variables (outside of setup/draw)
2. setup (once)
3. draw (loops over and over)

That's why these three examples generate different values. In the first example, the custom size has not been set yet, at the moment the two variables are initialised. Placing the call before or after setup textwise does not affect this order.

Code:
float x_0 = width / 2;
float y_0 = height / 2;

void setup(){
 size(640, 480);
 println(x_0); // prints 0
 println(y_0); // prints 0
}


Code:
void setup(){
 float x_0 = width / 2;
 float y_0 = height / 2;
 size(640, 480);
 println(x_0); // prints 50 (default window size is 100)
 println(y_0); // prints 50 (default window size is 100)
}


Code:
void setup(){
 size(640, 480);
 float x_0 = width / 2;
 float y_0 = height / 2;
 println(x_0); // prints 320
 println(y_0); // prints 230
}

Re: Using width and height
Reply #4 - May 10th, 2010, 3:52pm
 
Actually I suspect the answer bananapeels is actually looking for relates to variable scope.  This might help clarify the situation:

Code:
// global variable
float testValue = 1;

void setup(){
 size(640, 480);
 // this variable is local to setup and NOT the same as the global variable:
 float testValue = 3;
 println(testValue);
}

void draw() {
 
}

void mousePressed() {
 // so this prints the value of the global variable
 println(testValue);
}


If you declare a variable outside of setup it is global and can be accessed from any subsequent methods (e.g. setup(), draw() etc.).  If you declare it within setup() - i.e. by declaring the type first (e.g. 'float') - then it can only be accessed from within setup().

Since you're likely to want to reference width and height based variables beyond the scope of setup you need to declare the variables globally, but because width and height aren't set until size is called (and note that this should always be the first call within setup!) you actually populate the variables inside setup:

Code:
float x;
float y;

void setup(){
 size(640, 480);
 x = width / 2;
 y = height / 2;
}


Page Index Toggle Pages: 1