Loading...
Logo
Processing Forum
Hi, I am relatively new to programming, and I am learning through the book Learning Processing as well as experimenting and building stuff independently. I have a question for experienced programmers regarding variables and passing copies. My question is "Is it necessary for variables that pass as copies to be made global?"

Here is a concrete example of my problem:

int carX = width-100;
int carY = height-450;
int carLength = width/10;
int carWidth = height/20;
int wheel = carLength/4;

void setup () {
  size (800, 800);
  background (0, 100, 50);
}

void draw () {
  road ();
   carLooks (700, 350, 80, 40, 20, color (random(255), random(255), random(255)));
  carLooks (450, 350, 80, 40, 20, color (random(255), random(255), random(255)));
  carLooks (200, 350, 80, 40, 20, color (random(255), random(255), random(255)));
}


void road () {
  int roadX = 400;
  int roadY = 400;

  strokeWeight (5);
  stroke (255);
  fill (128);
  rectMode (CENTER);
  rect (roadX, roadY, width+5, height/4);

  // Lane dividing lines
  for (int i = 0; i <= width; i += 96) {
    line (i, roadY, i+48, roadY);
  }
}

void carLooks (int carX, int carY, int carLength, int carWidth, int wheel, color c) {

  // Car body
  noStroke ();
  fill (c);
  rect (carX, carY, carLength, carWidth, 50);
  noLoop ();

  // Car wheels
  fill (0);
  rect (carX-wheel, carY-wheel, wheel, wheel/2, 5);
  rect (carX+wheel, carY-wheel, wheel, wheel/2, 5);
  rect (carX-wheel, carY+wheel, wheel, wheel/2, 5);
  rect (carX+wheel, carY+wheel, wheel, wheel/2, 5);
}

I have tried to put the global variables (bold) in the block of code entitled "void carLooks" but I received an error message reading "duplicate local variable carX...". Is there a way to move the bold section to "void carLooks" without receiving this error message? It seems reasonable that this may not be possible, that these variables must remain global because the variables are being used in a block of code outside of "void carLooks", namely, "void draw", but I don't have a firm understanding on how passing copies works. The reason I would like to turn the global variables into local ones under "draw carLooks" is because I don't see myself reusing them in other blocks of code, and I am practicing how to be organized by keeping variables local whenever possible. If there is a way, then let me know how or refer me to a source, and if there is not, then that is okay too. 

Thank you all for your time.

Twitter: @eselotter

*edit*
It is also worth mentioning that the reason I am passing the copies of the parameters to "void draw" is so I can create multiple versions of the car. This is why I am avoiding using carLooks (); without parameters.

Replies(4)



the reason that you get an error when you copy the bold section into the function
carLooks is that in the function these names are already present: namely as the parameters.

Solution:
  • copy the bold section into the function carLooks
  • remove parameters from the function (similar to function road())

function looks like this: 

Copy code
  1. void carLooks () {

and you call it like this:
Copy code
  1. carLooks ();


Another answer
Another answer to this is the usage of class / object.
Then you would encapsulate all vars and functions related to car into one neat package (the class).
There is a tutorial for that.

Greetings, Chrisir   

If you need an answer, please send me a personal message since this forum doesn't notify.
The best solution would be to create a Car class, that would store the information for each kind of a Car object.
That way each Car object would be able to track its own set of variables for its own dimensions.

Copy code
  1. Car aCar, bCar, limo;

  2. void setup () {
  3.   size (800, 800);
  4.   rectMode(CENTER);
  5.   aCar = new Car( 700, 350, 80, 40, 20, color(200, 100, 100) );
  6.   bCar = new Car( 100, 450, 80, 40, 20, color(0, 128, 0) );
  7.   limo = new Car( 300, 450, 160, 40, 20, color(255) );
  8.   
  9. }

  10. void draw () {
  11.   background(0, 100, 50);
  12.   road();
  13.   aCar.draw();
  14.   bCar.draw();
  15.   limo.draw();
  16. }

  17. void road() {
  18.   int roadX = 400;
  19.   int roadY = 400;
  20.   strokeWeight (5);
  21.   stroke(255);
  22.   fill(128);

  23.   rect(roadX, roadY, width+5, height/4);
  24.   for (int i = 0; i <= width; i += 96) {
  25.     line(i, roadY, i+48, roadY);
  26.   }
  27. }

  28. class Car {
  29.   float x, y, w, h, e;
  30.   color c;
  31.   Car(float _x, float _y, float _w, float _h, float _e, color _c) {
  32.     x = _x;
  33.     y = _y;
  34.     w = _w;
  35.     h = _h;
  36.     e = _e;
  37.     c = _c;
  38.   }
  39.   void draw() {
  40.     // Body
  41.     noStroke();
  42.     fill(c);
  43.     pushMatrix();
  44.     translate(x,y);
  45.     rect(0,0, w, h);
  46.     // Wheels - fixed a bit
  47.     fill(0);
  48.     translate(e-w/2,-h/2);
  49.     rect(0,0, e, e/2 );
  50.     translate(w-e-e,0);
  51.     rect(0,0, e, e/2 );
  52.     translate(0,h);
  53.     rect(0,0, e, e/2 );
  54.     translate(-w+e+e,0);
  55.     rect(0,0, e, e/2 );
  56.     popMatrix();
  57.   }
  58. }

Beware of using width and height at declaration site of global variables.
See What are setup() and draw()? which explains why.