Use a variable throughout multiple functions

edited October 2014 in Questions about Code

Howdy,

I am trying to understand how to use variables throughout a sketch and am having a slight bit of trouble understanding it. I would like to use the mouseColor and noSteps throughout my sketch. Here's what I have:

float mouseColor = 255;
float noSteps = 20;

void setup(){
  background(0);
  size(800,800);
  colorMode(HSB);
  float noSteps = 10;
}

void drawStar(float steps){
  strokeWeight(1);
  smooth();
  int hWidth = width/2;
  int hHeight = height/2;
  line(0,hHeight,width,hHeight);
  line(hWidth,0,hWidth,height);
 for (int i = 0; i < hWidth; i += steps) { //Q1
    line(hWidth, i, hWidth+i, hHeight);
}
  for (int i = 0; i < hWidth; i += steps){ //Q2
    line(hWidth,i,hWidth-i,hHeight);
  }

  for (int i = 0; i < hWidth; i += steps){ // Q3
    line(hWidth,height-i,hWidth+i,hHeight);
  }
  for (int i = 0; i < hWidth; i += steps){ // Q4
    line(hWidth,height-i,hWidth-i,hHeight);
  }
}


void drawBorder(float steps){
  int hWidth = width/2;
  int hHeight = height/2;
  for (int i = 0; i < hWidth; i += steps){ // Q1
    line(hWidth+i, 0,width, i);
  }
  for (int i = 0; i < hWidth; i += steps){ // Q2
    line(hWidth-i, 0,0, i);
  }
  for (int i = 0; i < hWidth; i += steps){ // Q3
    line(0, hHeight+i,i,height);
  }
  for (int i = 0; i < hWidth; i += steps){ // Q4
    line(hWidth+i, height,height, height-i);
  }  
}

void helpText(){
  float mouseColor = map(mouseY,0,height,0,255);

  String mousePos = str(mouseY);

  text("MouseY, "+mousePos+", color "+mouseColor,50,50);

  float noSteps = map(mouseX,0,width,10,30);
  text(noSteps,50,80);
}

void draw(){
  background(0);

  helpText();
  stroke(mouseColor,255,255);
  drawStar(noSteps);
    stroke(255-mouseColor,255,255);
  drawBorder((30+1)-noSteps);
}

So, as it stands now, the mouseColor and noSteps stay at 255 and 20, as determined at the top (these are "global variables", yes?).

BUT - I want to determine them in my draw() loop - how can I do that? I have gotten it working if I move everything in the "helpText()" function to the draw() loop. But I would like to keep what's in draw() to a minimum, and use functions to help do so. Is this possible? Does my question make sense?

Another question, which is the same issue pretty much, is how can I code it so I don't have to set "hHeight" and "hWidth" in each function that uses them?

Thanks so much for any help, let me know if I can clarify anything!

Answers

  • edited October 2014 Answer ✓

    You're redeclaring them as local variables everywhere! Thus overshadowing them! L-)

  • edited October 2014

    D'oh! I think I see. So, I can set them globally at the top, but remove the 'float' or 'int' part before resetting them to something?

    Be right back, will try that.

    Edit: okay, I got it working with the Text part, but can't figure out how to declare the hHeight and hWidth globally. If I place in Setup, I get the "no variable named hHeight" error. I place under Setup, not in a function, and then nothing is drawn...

  • Ok I got it working, but my question now is: Why do I have to place hWidth and hHeight in the draw() loop? If I place them anywhere else, none of the functions can read the values.

    Here's the code with the update - does this all look correct?

        float mouseColor, noSteps;
        int hHeight, hWidth;  
    
        void setup(){
          background(0);
          size(800,800);
          colorMode(HSB);
        }
    
        void drawStar(float steps){
          strokeWeight(1);
          smooth();
          line(0,hHeight,width,hHeight);
          line(hWidth,0,hWidth,height);
         for (int i = 0; i < hWidth; i += steps) { //Q1
            line(hWidth, i, hWidth+i, hHeight);
        }
          for (int i = 0; i < hWidth; i += steps){ //Q2
            line(hWidth,i,hWidth-i,hHeight);
          }
    
          for (int i = 0; i < hWidth; i += steps){ // Q3
            line(hWidth,height-i,hWidth+i,hHeight);
          }
          for (int i = 0; i < hWidth; i += steps){ // Q4
            line(hWidth,height-i,hWidth-i,hHeight);
          }
        }
    
      void drawBorder(float steps){
    
          for (int i = 0; i < hWidth; i += steps){ // Q1
            line(hWidth+i, 0,width, i);
          }
          for (int i = 0; i < hWidth; i += steps){ // Q2
            line(hWidth-i, 0,0, i);
          }
          for (int i = 0; i < hWidth; i += steps){ // Q3
            line(0, hHeight+i,i,height);
          }
          for (int i = 0; i < hWidth; i += steps){ // Q4
            line(hWidth+i, height,height, height-i);
          }  
        }
    
        void drawCircles(){
         ellipse(hWidth,hHeight,20,20);
        }
    
        void drawText(){
          String mousePos = str(mouseY);
          text("MouseY, "+mousePos+", color "+mouseColor,50,50);
        }
    
    
        void draw(){
          hWidth = width/2;   // why can't I declare these anywhere else?
          hHeight = height/2;
          noSteps = map(mouseX,0,width,10,30);
          mouseColor = map(mouseY,0,height,0,255);
    
          background(0);
          drawText();
          stroke(mouseColor,255,255);
          drawStar(noSteps);
          stroke(255-mouseColor,255,255);
          drawBorder((30+1)-noSteps);
    
          text(noSteps,50,80);
    
        }
    
  • hWidth = width/2; // why can't I declare these anywhere else?

    You can declare variables anywhere. But they become local to the scope where it happened!

  • Best place to initialize variables in within setup():
    https://processing.org/reference/setup_.html

  • edited October 2014

    Hm I understand that part - if I remove those to lines from draw and place above (or below) setup, when I run the sketch, nothing appears (as if hHeight and hWidth are 0, I suppose). And I removed the initial 'blank' declaration at the very beginning.

    But thanks for your help, it clarified the bigger issue/question I had!

    Edit: I thought so too...but if I place int hWidth = width/2; int hHeight = height/2;

    in Setup(), in the drawStar() function get the error "Cannot find anything named hHeight" when running.

  • edited October 2014

    Best practices:

    • Make size() the 1st statement within setup() always.
    • Now you can initialize any variables which depend on width & height within setup().
  • Thanks for the note on size(). I put that first, and am still getting the error.

    float mouseColor, noSteps;
    
    void setup(){
      size(800,800);
      background(0);
      colorMode(HSB);
      int hWidth = width/2;
      int hHeight = height/2;
    }
    
    
    
    void drawStar(float steps){
      strokeWeight(1);
      smooth();
      line(0, hHeight, width, hHeight);  // error shows up here.
      line(hWidth,0,hWidth,height);
     for (int i = 0; i < hWidth; i += steps) { //Q1
        line(hWidth, i, hWidth+i, hHeight);
    }
      for (int i = 0; i < hWidth; i += steps){ //Q2
        line(hWidth,i,hWidth-i,hHeight);
      }
    
      for (int i = 0; i < hWidth; i += steps){ // Q3
        line(hWidth,height-i,hWidth+i,hHeight);
      }
      for (int i = 0; i < hWidth; i += steps){ // Q4
        line(hWidth,height-i,hWidth-i,hHeight);
      }
    }
    

    "Cannot find anything named 'hHeight'" - and highlights the 'line(0,hHeight,width,hHeight); ' part...

  • Answer ✓

    You're declaring both inside setup()! You're supposed to initialize them instead! [-(

  • You're declaring both inside setup()! You're supposed to initialize them instead!

    AHA! Duuhhhh. I am suppose to declare them outside globally, but initialize them in Setup!

    So, DECLARE variables I want to use throughout the sketch, and INITIALIZE those variables where necessary in the sketch.

    Thanks so much for your help!!

  • edited October 2014

    That's it! Although notice that initialize refer to the 1st value assigned to a variable.
    After that it's merely called assignment afterwards. (*)

    Most of the time, we can both declare and initialize a variable in the same statement.
    However in Processing, both width & height only get their values after size()!

    Another 1 in the same vein is sketchPath! :|

  • edited October 2014

    If you're still here - I am trying to look up some best practices for Processing, but Googling around is a bit tricky. Do you have any sites or queries I should look up?

    (Or, what key words to include to make sure the Processing computer code/software pops up ...not generic best practices for processing invoices :P)

  • It's hard to search for something called "Processing", sorry! :(
    Don't forget that Processing is a framework, whose main language is Java!

    For alternative forums, you can try:

    There are some tutorials in the Processing.org site too:

  • edited October 2014

    You might also try StaticVoidGames there are some nice tutorials and the site owner is a member of this forum.

  • Thanks GoToLoop and quark! :D

Sign In or Register to comment.