MV Problem with conditional branch for showing pictures

Good day!
I was trying to create simple plugin for testing purposes: window in Scene_menu which will show picture based on a variable value. If it's 1, picture '123' will be shown. If it's 2, picture '1234', if it's 3, picture '12345', if none of the above, picture '12'.

Here is the part of the code in question (everything else is just stuff to create window itself).

Window_Test.prototype.drawPicture = function(filename, x, y) { var bitmap = ImageManager.loadPicture(filename); this.contents.blt(bitmap, 0, 0, bitmap._canvas.width, bitmap._canvas.height, x, y); }; Window_Test.prototype.refresh = function() { if ($gameVariables.value(0001) === 1) { this.drawPicture ("123", 0, 0, 200, 200); } else if ($gameVariables.value(0001) === 2) { this.drawPicture ("1234", 0, 0, 200, 200); } else if ($gameVariables.value(0001) === 3) { this.drawPicture ("12345", 0, 0, 200, 200); } else { this.drawPicture ("12", 0, 0, 200, 200); } };

Window just don't appear. Changing variable value changes nothing.
Just in case, I've tested plugin with 'drawText' instead of 'drawPicture'. It works perfectly.

I'm a little lost here. Can you explain, what am I doing wrong?
UPD: Sorry for the false alarm!
I don't know what I did, but it works now. I guess I skipped one '}' in my file while testing and while compulsively retyping the code added it... somewhere.
You should make sure not to put leading zeros in front of your numbers like you have there.

That causes JavaScript to read them as octal values, which is not what you want. With the value of 1 it just happens to be what you want, but any higher numbers will break your code.

I guess I skipped one '}' in my file while testing and while compulsively retyping the code added it... somewhere.
there are a few ways to help you avoid that.

1 - don't use extra characters you don't need to. In your case, none of those braces are actually necessary after your if/else ifs, because you only have one command after them. No need to create a code block.

2 - use better indentation to help you see code blocks when you do need to make them. Your closing braces for an if statement shouldn't be at the same indentation as your closing brace for the function.

3 - I don't know what you're using to type your code, but anything from Notepad++ to more specific IDEs will automatically color/highlight parentheses and braces so you can easily see if something is left open.

Edit: Sorry, I meant to type this as an edit and it went into a separate reply by accident.
There's another potential problem. Block-transfer (blt) requires the image to be in memory. However, files are loaded asynchronously from disk. So your approach, as-is, will typically fail to draw the image the first time around.

A couple of possible solutions for this:
  1. Attach the drawPicture method to the bitmap as a "load listener". The engine will run all attached load listeners when the image finishes loading (or immediately, if the image is already in memory at that point). Example:
    JavaScript:
    Window_Test.prototype.drawPicture = function(filename, x, y) {
      var bitmap = ImageManager.loadPicture(filename);
      bitmap.addLoadListener(function(bmp) {
        this.contents.blt(bmp, 0, 0, bmp.width, bmp.height, x, y);
      }.bind(this));
    };

  2. Load the image ahead of time, e.g. in the scene's create method. If necessary, a plugin such as Community_Basic (present by default in a new MV project) will let you increase the image cache size via the Plugin Manager.
You should make sure not to put leading zeros in front of your numbers like you have there.

That causes JavaScript to read them as octal values, which is not what you want. With the value of 1 it just happens to be what you want, but any higher numbers will break your code.
Oh, YES! That's what I did! Thank you! At least now I know what to look for in case of future mistakes...

Also thanks for other advices. I started learning JS few days ago and just used basic Notepad. I'll definitely try better programs now.

There's another potential problem. Block-transfer (blt) requires the image to be in memory. However, files are loaded asynchronously from disk. So your approach, as-is, will typically fail to draw the image the first time around.
Thanks, I was wondering about that one as well. I will try.

UPD. Yep! Adding load listener lines helped a lot! I thought there was something wrong with the refresh method, but I never would've discovered that solution. So thank you very much!
Great! Hopefully it's all OK now~
Great! Hopefully it's all OK now~
Yes! Everything is just fine! Thanks again.

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-conditional-branch-for-showing-pictures.156131/