RMMV Problem with conditional branch for showing pictures

Prince_Eric

Villager
Member
Joined
Aug 22, 2019
Messages
16
Reaction score
2
First Language
Russian
Primarily Uses
RMMV
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?
 
Last edited:

Prince_Eric

Villager
Member
Joined
Aug 22, 2019
Messages
16
Reaction score
2
First Language
Russian
Primarily Uses
RMMV
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.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
8,918
Reaction score
6,726
First Language
English
Primarily Uses
RMMV
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.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
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.
 

Prince_Eric

Villager
Member
Joined
Aug 22, 2019
Messages
16
Reaction score
2
First Language
Russian
Primarily Uses
RMMV
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!
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,647
Reaction score
3,779
First Language
EN
Primarily Uses
RMMZ
Great! Hopefully it's all OK now~
 

Latest Threads

Latest Posts

Latest Profile Posts

With this, I'm gonna have a good night sleep
Damn. We’ve gotten so close to 0 reports, 0 approvals a few times over the past couple weeks. Does it matter? No. Is it realistic. No? Do I rely on your reports and want everyone to keep posting. Yes. Do I 100% games? …sigh… Yes.

Ah yes, all three of my moods all at once.
Here's a tutorial I did on how I made my Leonardo A.I.-assisted artworks for my game's recent update. :rhappy:

Disclaimer: This is meant to be a band-aid solution for people like me who aren't good artists or don't have the financial means to hire an amazing artist. If you have the means, please buy commissions and support your fav artists.:yhappy:

Forum statistics

Threads
131,754
Messages
1,222,989
Members
173,515
Latest member
ThisGuyErik
Top