Trying to Reduce the Number of Global Variables

int x, y;
int xSpeed = 0;
int ySpeed = 0;
int diam = 50;
int xConstr;
int yConstr;

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

void draw()
{
  drawBackground();
  
  drawObject();
  
  moveObject();
  
  addMotion();
}

void drawBackground()
{
  background(255);
}

void drawObject()
{ 
  constrainObject();
  ellipse(xConstr, yConstr, diam, diam);
}

void moveObject()
{ 
  x = x + xSpeed;
  y = y + ySpeed;
}

void addMotion()
{
  
  if(keyPressed)
  {
    if(keyCode == RIGHT)
    {
      xSpeed = 1;
    }
    if(keyCode == LEFT)
    {
      xSpeed = -1;
    }
    if(keyCode == UP)
    {
      ySpeed = -1;
    }
    if(keyCode == DOWN)
    {
      ySpeed = 1;
    }
  }
  else
  {
    xSpeed = 0;
    ySpeed = 0;
  }
}

void constrainObject()
{
  yConstr = constrain(y, diam / 2, height - diam / 2);
  xConstr = constrain(x, diam / 2, width - diam / 2);
}

That’s the code i’m currently working on. I need to reduce the amount of global variables and create more passing arguments but i’m not really sure how to go about it.

Thanks!

Have a look at this tutorial: https://processing.org/tutorials/objects/
Then attempt to make some changes. If you run into a road block, ask here and provide your attempt.

Kf

Hi @QuazArxx,

You can format your code by using the </> button when you paste some in the forum. Try and edit your post !

As for your problem why do you need to get rid of those global variables ? In your case, since you are using them in draw, you can’t really do otherwise.

You can use classes as @kfrajer mentioned to order your code a bit more but you’ll still have some global variables.

I had already attempted to move variables around within functions first and nothing seemed to work so I moved them all to global.

I don’t technically HAVE to, but it’s for a school project and in order for me to get the maximum grade possible I need to eliminate global variables and use passing arguments in my functions.

Based on what is taught in your school, what options do you have?

What have you tried it? Show your attempt.

Kf

I’m not sure how to show my attempt if whatever I did I had errors because it couldn’t find the variables because they weren’t global.

The only option I have otherwise is leave it all global with no arguments in the functions and get a score as high as 8/10****

int x, y;
int xSpeed = 0;
int ySpeed = 0;
int diam = 50;
int xConstr;
int yConstr;

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

void draw()
{
  drawBackground();
  
  drawWin();
  
  drawObject();
  
  moveObject();
  
  resetObject();
  
  addMotion();
}

void drawBackground()
{
  background(0);
  rect(20, 30, 280, 320);
}

void drawWin()
{
  int minHeight = 250;
  int maxHeight = 330;
  
  fill(0, 255, 0);
  noStroke();
  rect(280, 250, 20, 80);
}

void drawObject()
{ 
  fill(255);
  stroke(10);
  constrainObject();
  ellipse(xConstr, yConstr, diam, diam);
}

void moveObject()
{ 
  x = x + xSpeed;
  y = y + ySpeed;
}

void resetObject()
{
  if(x > 300 - diam / 2 || x < 20 + diam / 2)
  {
    x = width / 2;
    y = height / 2;
  }
  if(y > 350 - diam / 2 || y < 30 + diam / 2)
  {
    x = width / 2;
    y = height / 2;
  }
}

String printWin()
{
  if(drawWin.minHeight > y < drawWin.maxHeight)
  {
    text("You win!", 100, 100);
  }
}
    

void addMotion()
{
  
  if(keyPressed)
  {
    if(keyCode == RIGHT)
    {
      xSpeed = 1;
    }
    if(keyCode == LEFT)
    {
      xSpeed = -1;
    }
    if(keyCode == UP)
    {
      ySpeed = -1;
    }
    if(keyCode == DOWN)
    {
      ySpeed = 1;
    }
  }
  else
  {
    xSpeed = 0;
    ySpeed = 0;
  }
}

void constrainObject()
{
  xConstr = constrain(x, 20 + diam / 2, 300 - diam / 2);
  yConstr = constrain(y, 30 + diam / 2, 350 - diam / 2);
}


drawWin() and printWin() are 2 new functions I added. In printWin if I try to call the variables declared in drawWin, I get an error stating “The variable ‘drawWin’ does not exist”. Of course it doesn’t exist because it’s not a variable.

Also yes I know I don’t have a return for printWin just yet. I was starting to set it up til i ran into the error stated.

You can’t use drawWin.minHeight or drawWin.maxHeight, it is not how it works.

You should read about variables scope: https://processing.org/examples/variablescope.html

For your problem you have basically 2 ways of sharing your variables:

  • Declare them in the global scope (outside any function) - wich you try to avoid
  • Use parameters for function

Let’s say I want to code an addition function that takes 2 numbers and add them together. Then my function would look something like this:

float addition(float a, float b) {
    return a + b;
}

This way, I can send numbers to that function from another part of the code:

void setup() {
    float num1 = 2.0;
    float num2 = 5.0;

    float result = addition(num1, num2); // Result will be 7.0
}
1 Like

I know about scoping but that’s why i’m confused Even with what you tell me i’m not sure how I can do that with my code. It’s getting difficult to think about this code due today. with all the variables declared in global, i’m not sure how to make those available only in the functions that they’re used in.