global variables - set int/float value in setup()

I found an answer to my original question here (https://forum.processing.org/one/topic/setting-a-variable-to-width-2-or-height-2.html) but now I have some other questions

1) what's the difference between having int x = 1; at the beginning and in setup()?

2) does int x; (not having a value) mean int x=0?

Thanks for your help.

Answers

  • Both width & height only got a valid value after size() is called! :-B

  • edited April 2017

    Hi GoToLoop Thanks so as I understand now no matter you write int = width after size(); Processing will still run int first? ( because I thought it's executed line by line..)

    in this example the ellipse won't be in the middle

    void setup() {
    size(800,600);
    }
    int x = width/2;
    int y = height/2;
    void draw() {
     ellipse(x,y,50,50);
    }
    
  • int x, y;
    
    void setup() {
      size(800,600);
      noLoop();
      fill(#FFFF00);
    
      x = width>>1;
      y = height>>1;
    }
    
    void draw() {
      background(0);
      ellipse(x, y, width>>1, height>>1);
    }
    
  • Answer ✓

    This below is the proper way to do things. Notice width and height are variables from the PApplet and they are only init after size() gets executed. To be safe, you access those values within any functions provided to your sketch.

    More info: https://processing.org/reference/size_.html

    Kf

    int x;
    int y;
    
    void setup() {
     size(800,600);
     x = width/2;
     y = height/2;
    }
    
    void draw() {
     ellipse(x,y,50,50);
    }
    
  • What you tried to do here:

    void setup() {
      size(800,600);
    }
    
    int x = width / 2;
    int y = height / 2;
    
    void draw() {
      ellipse(x,y,50,50);
    }
    

    won't work. Anything not inside a method will be executed when the class is instantiated, regardless of where it is in the actual file. So those two lines in the middle will run before setup and width and height will be zero.

  • Answer ✓

    In this example the ellipse() won't be in the middle.

    Even though x & y are placed between setup() & draw(), they're outside any functions.

    So, when variables are declared outside functions and curly {} blocks, they're called fields.

    They're executed even before their class' constructor! @-)

    Also, it doesn't matter whether draw() is typed in before setup() or not; b/c they're called back by sketch's "Animation Thread". B-)

  • So those two lines in the middle will run before setup and width and height will be zero.

    I thought they would be 100 rather than 0?

    thanks!

  • thanks all

    another question- does int x; mean int x=0;?

  • Variables are always initialized by Java if you don't do it yourself. Reference to objects are set to null until you call the constructor using the new keyword.

    I prefer the practice of initializing everything instead of something doing it for you. A better practice is to init everything in setup but it is more of an user preference.

    So in your question, x is set to zero. You could also use println(x) :-B

    Kf

  • Answer ✓

    Does int x; mean int x=0;?

    Yes for fields. But not for local variables. :-B

  • edited April 2017 Answer ✓

    Go to "Default Values" section in order to know field's initial default values for all Java datatypes:
    http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  • void setup() {
    size(800,600);
    }
    int x = width/2;
    int y = height/2;
    void draw() {
     ellipse(x,y,50,50);
     println(x);
    }
    

    I printed x I can see in the console it's 50? I guess cuz the default size is 100x100?

  • thanks guys!

  • Answer ✓

    println(DEFAULT_WIDTH, DEFAULT_HEIGHT); are the initial values for width & height before size().

Sign In or Register to comment.