Hi, I am not sure if this is a bug or a feature. Basically, you cannot load files before setup().
Example code
PImage img = loadImage("clrPic.jpg");
int p[][];
int v[][];
void setup() {
size(600, 800);
p = new int[width][height];
v = new int[width][height];
}
void draw() {
background(0);
//image(img, 0, 0);
}
In most cases, load all images in setup() to preload them at the start of the program. Loading images inside draw() will reduce the speed of a program. Images cannot be loaded outside setup() unless they’re inside a function that’s called after setup() has already run.
So this is definitely not a bug. But I don’t know if Processing enforces that or it cannot load an image because the setup function does something in particular (setting the sketch path? As stated in the error…)
Just to elaborate – load images and set sketch size in setup() or settings(), as described above. However, this is a designed limitation for full sketches with setup() etc. – it isn’t a limitation related to the sketch path itself. Unsaved sketches are just sketches in a temporary directory that have not yet been renamed and moved into the main file path, and immediate mode sketches without setup() can always load images just fine, whether or they are saved or “unsaved” (which is really just located in a temporary directory).
Try opening a new, unsaved sketch window, enter this snippet of code and run without saving:
That is the sketch location and name before it is saved. Open that folder and copy the image inside. Then try loading the local image from the “unsaved” sketch with a relative path.
The image loads fine, even from an unsaved sketch in a tmp folder.
So: this is a Processing requirement for how a sketch with setup() etc. loads resources – it doesn’t have anything to do with sketch paths or a sketch being “unsaved.”