conventions re: scope, where to initialize variables

Hi,

I understand that if you want to be able to use variables globally you have to declare them at the top of the program. If you declare them inside a method they can only be used inside that method.

What I dont understand is when/where I should initialize variables, whether there are conventions regarding that. Should I initialize them in the first method that uses them, or in setup() or at the top?

Cheers,

Tagged:

Answers

  • edited May 14 Answer ✓

    For local variables - those defined inside a method - it's probably best to initialize them when you declare them. The one exception here is for arrays of values. If you're going to use a loop of some kind to initialize the values in the array, I would do the loop after all your local variables are declared.

    For global variables, I would suggest you write your own reset() function to initialize them. Then call this reset() function from setup(). This way, if your sketch every gets to a point where it needs to "start over", you can just call reset(). This convenience outweighs all other benefits IMHO. Of course, if there are globals that WON'T need to get reset, then you can set their values in setup. Also, if you'e defining constants, well, those (should) have to be done when the variable is declared.

    Summary:

    final int NEVER_CHANGE = 42; // Constant.
    String[] cool_people; // Won't need resetting.
    float half_width; // Depends on width, so don't initialize here!
    float xPosition, yPosition; // Will need resetting later!
    
    void setup(){
      size(400,400);
      cool_people = { "TfGuy44" };
      half_width = width / 2.0;
      reset();
    }
    
    void reset(){
      xPosition = half_width;
      yPosition = 200;
    }
    
    void draw(){
      int[][] randomValues = new int[3][3]; // Array - initialize later.
      boolean hit_detected = false; // Local variable, initialize now.
      for( int i=0; i<3;i++){
        for( int j=0; j<3;j++){
          randomvalues[j][i] = int(random(20));
        }
      }
      // The rest of draw...
    }
    
    // Other functions or whatever...
    
    void keyPressed(){
      if( state == GAME_OVER && key == ' '){
        reset(); // Example of starting over!
      }
    }
    
  • Global variables that require values from the PApplet needs to be init in setup.

    Kf

  • edited May 14

    Those "problematic" PApplet's system variables are basically these 7:

    1. width
    2. height
    3. displayWidth
    4. displayHeight
    5. pixelWidth
    6. pixelHeight
    7. sketchPath (accessed via sketchPath() btW)
    • If you've got anything which depends on any of those 7 system variables, they should be initialized in setup(), or at the most earliest in settings().
    • Notably, both width & height should only be accessed after calling size(), otherwise they're both 100. @-)
    • And exceptionally, both pixelWidth & pixelHeight don't seem to work even within settings().
    • All of the other 5 system variables can be used within settings() though.
    • Everything else is relatively safe to be initialized before settings()! \m/
Sign In or Register to comment.