Opening numbered images

Hi,

in a folder I saved three .png files:
“1.png”, “2.png”, “3.png”

In the Processing code I use a constant:
int A = 3;

Now: I would like open an “A.png” image , with A set by the variable above.
So, for example:
int A = 3; img = loadImage("A.png"); => the "3.png" image opens

So a code like this (which obviously doesn’t work):

size(400,400);
int A = 3;
PImage img;
img = loadImage("A.png");
image(img, 0, 0);

By starting the sketch, the “3.png” image should open.
Any tips?
Thank you,
f

img = loadImage("A.png");

Anything inside of those quotation marks will be treated as a string only. So that “A” is not seen as a variable, but instead is just a character in a longer string.

Instead, concatenation is your friend here.

You use the “+”-symbol to concatenate/combine two or more parts together. In your case, the variable part (A) and the string part (“.png”). Slap the + between the two parts and you get:

img = loadImage(A+".png");

On runtime, this concatenation will resolve into “3.png”. (If the value of A is 3).

2 Likes

Also, this is not a constant. The value of a constant doesn’t (or isn’t supposed to) change while running the sketch.
For your use-case, this is a variable, because it’s meant to hold variable/changing values.

I’m obviously being nit-picky here, but better to get the basic wordings down early and prevent confusion down the line.

2 Likes

Yes, this is the solution.
Thank you very much,
f

You are right, I was imprecise in writing.
“A” must be a variable, because it will then receive numbers via OSC, stored in “A” for use in opening images.
Bye,
f