r/processing • u/ONOXMusic • Sep 17 '23
Help request Why does this code for loading images into PImage array not work?
PImage[] pieces = new PImage[0];
void setup() {
size(600,400);
loadPieces('w'); // Loads white pieces
loadPieces('b'); // Loads black pieces
}
void loadPieces(char c) {
char[] piece_ref = { 'P', 'B', 'N', 'R', 'Q', 'K' };
String p;
for (int i = 0; i < 6; i++) {
p = c+""+piece_ref[i];
pieces = append(pieces, loadImage(p+".png"));
}
}This returns "Type mismatch: cannot convert from Object to PImage[]"
1
Upvotes
1
u/takahami Sep 17 '23 edited Sep 17 '23
Syntax looks legit.
I couldnt test it but maybe the way you declare pieces with that [0] might be the problem... Just a wild guess.
Maybe processing got a problem with the loadimage as it expects an pimage and yeah, loadimage returns an pimage, but maybe you want to load the Image into a temporary / local pimage variable and put that one into the append function.
That's another wild guess.
As I said, syntax looks legit except that I got a funny feeling about the declaration.
2
u/andrewcooke Sep 17 '23
try
the docs day you need to do this in some cases (I don't really understand why the example doesn't include it) https://processing.org/reference/append_.html
1
u/[deleted] Sep 18 '23
holy shit this is a horrible loading function from hell
-2
u/kstacey Sep 17 '23
Function should be returning an array of P images instead of a global variable. Not sure why you need to include +""+. Not sure why you have [0] in the variable either, I don't think that's the proper way to instantiate an array in processing. For loop should use the length of your character array rather than 6. It looks like you are very much learning to program still