Use only numbers (not variables) for the size() command.

Okay, this has thrown my head for a spin. When I'm running my sketch I'm loading an image using PImage, and using the dimensions from that image I set the size of the sketch. Like so:

PImage image;

void setup() {
  image = loadImage("image.jpg");
  size(image.width, image.height);
}

However, running that under Processing 3.0 spits out the following:

The size of your sketch could not be determined from your code. Use only numbers (not variables) for the size() command. Read the size() reference for more details.

Now, from what I've figured out you need to use void settings() to use variables in size() but here is the catch 22: the image can be be loaded earliest in setup() and settings() need to be called before setup().

I.e. neither of the following code snippets are valid solutions:

PImage image;

void settings() {
  image = loadImage("image.jpg");  // can't load image before setup()
  size(image.width, image.height);
}

void setup() {
}

nor:

PImage image;

void setup() {
  image = loadImage("image.jpg");
}

void settings() {  // can't call settings() after setup()
  size(image.width, image.height);
}

Is there a way around this that I'm not seeing?

Sign In or Register to comment.