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 › Global variables...
Page Index Toggle Pages: 1
Global variables... (Read 1395 times)
Global variables...
Dec 23rd, 2008, 6:58am
 
...how do you declare declare them inside a function?
Re: Global variables...
Reply #1 - Dec 23rd, 2008, 7:10am
 
Well, you kind of can't do that in Java.  Liberal use of global variables is often discouraged as a general programming practice.

You COULD make a class with static members as a workaround in *Java*, but implementation will be extremely ugly.  

To make the matter even worse, given the fact that classes in Processing code is all inner class, you also can't use static members to emulate global variables.  Same thing goes to singleton pattern using static variables.

Basically, you'll have to declare it outside the functions in processing code.

What kind of problem would you like to solve?  Any example?
Re: Global variables...
Reply #2 - Dec 23rd, 2008, 7:47am
 
Here's the story:
I am learning processing together with actionscript. In processing, I made a little ball that goes straight to any direction then bounces on the edges of the screen. Then I made two balls like that. And then I wanted to make a lot of those. My idea was to use a "for()" iteration to declare a bunch of variables for each ball, like position, velocity and color, in a way that I could reach them later on the program. But the variables declared inside the "for()" iteration wouldn't be accessible, because they would be local. Then I remembered that there was a way to declare global variables in AS, and I thought that I could do it in processing too, since those two languages are so close.

Another thing I would have to do is call each variable for each ball with a different name, and I was aproaching that using the iteration variable as a part of the name of each variable I would declare for the balls. That apparently wasn't working either, so, here comes another question:
Is it possible to declare a variable of wich part of the name is the value of another variable?
Re: Global variables...
Reply #3 - Dec 23rd, 2008, 8:08am
 
You come up with the hard questions Smiley

In a word, not really (ok, two words).  You can't assign the name of the variable dynamically, as they are not being evaluated at runtime.  On the other hand, you CAN use something like HashMap to create a collection of variables that afford that type of flexibility.  I've used that in a very special case where the graphic element behavior algorithm can be edited during runtime.  It works surprisingly well...well, at least until I got stuck using embedded jython interpreter for advanced cases (man, is that thing slow! Sad I know, I can use javascript in Java6)...but I digress.

Probably none of the above is necessary in your case.  Would the framework below work?  It is pretty much a boilerplate.

What you may want to do is to create a Ball class, that looks like:
Code:

class Ball{
 String name;
 PVector location;
 PVector velocity;
 
 Ball(String name, PVector location, PVector velocity){
   this.name = name;
   this.location = location;
   this.velocity = velocity;
 }

 void draw(){
   //draw code
 }
 void update(){
   //bouncy algorithm
 }
}



...and in your main code,

Code:


ArrayList balls;

void setup(){
 balls = new ArrayList();
 //add balls like:
 balls.add(new ball("ball_1", new PVector(10, 10), new PVector(10,10));
 balls.add(new ball("ball_2", new PVector(20, 20), new PVector(10,-10));
}


void draw(){

 //...some kind of routine

 for(int i = 0; i < balls.size(); i++){
    balls.get(i).draw();
    balls.get(i).update();
 }
}

Re: Global variables...
Reply #4 - Dec 23rd, 2008, 5:25pm
 
I know really few about classes. Maybe it's a nice oportunity to learn!

I will read more tutorials and stuff. Just wanted to know two things: what is this PVector, is it a type of variable? I was actually using two variables for position, and two for velocity. And what is this Code:
void update() 

method? Is it a function you declared yourself?
Re: Global variables...
Reply #5 - Dec 23rd, 2008, 5:58pm
 
Yes, basically a class is a way to organize your code and data in a way that is meaningful to the problem.  So a class can contain a bunch of class variables (called members) and class functions (called methods).  I won't go into detail here, because there are plenty of excellent materials written on this topic.  

There really isn't anything special about it from what you are already doing, but it will make it very easy to handle all the problems that you have mentioned so far.

Why not start here?  If you can post some example code you want to make into object-oriented code, these can be converted to fit into the framework mentioned.  It might help explain how class works.


And yes, I did make up that class function ( update() ).
Page Index Toggle Pages: 1