RMMV Most effective way to wait for ImageManager.loadBitmap to completely load an image?

subengari

Veteran
Veteran
Joined
Dec 12, 2020
Messages
36
Reaction score
14
First Language
king's english
Primarily Uses
RMMV
Behold the following JS snippet:
bmp_isReady.PNG
It works partially upon execution, running the script commands between the bmp.isReady clause for a few of the PNGs that I am processing... however a good chunk of other PNGs aren't processed at all.

Is there someway to completely wait for ImageManager.loadBitmap to do its thing before running the commands between the bmp.isReady clause?

This is all happening upon loading of the game, so I am not really concerned if there is a waiting time for the operation to complete- the important thing is that the commands are run on each and every PNG before proceeding.

Thanks in advance!
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,480
Reaction score
2,630
First Language
EN
Primarily Uses
RMMZ
You can use Bitmap#addLoadListener. This will attach a callback function which will be run once the bitmap is done loading (cf rpg_core.js). I think it goes something like this (untested):
JavaScript:
const onLoadImage = function() {
  this._context.getImageData(0, 0, this.width, this.height);
  // etc
};

const bmp = ImageManager.loadBitmap(folder, image);   // initiate async load request
bmp.addLoadListener(onLoadImage.bind(bmp));           // bind function to bitmap to run on load
 

subengari

Veteran
Veteran
Joined
Dec 12, 2020
Messages
36
Reaction score
14
First Language
king's english
Primarily Uses
RMMV
You can use Bitmap#addLoadListener. This will attach a callback function which will be run once the bitmap is done loading (cf rpg_core.js). I think it goes something like this (untested):
JavaScript:
const onLoadImage = function() {
  this._context.getImageData(0, 0, this.width, this.height);
  // etc
};

const bmp = ImageManager.loadBitmap(folder, image);   // initiate async load request
bmp.addLoadListener(onLoadImage.bind(bmp));           // bind function to bitmap to run on load

Thanks, @caethyril - the example you give doesn't work outright... but I will read up on addLoadListener and see if I can use it to work out my problem. I appreciate the suggestion!
 

subengari

Veteran
Veteran
Joined
Dec 12, 2020
Messages
36
Reaction score
14
First Language
king's english
Primarily Uses
RMMV
@caethyril hmmm... addLoadListener seems less effective than my original code... I get no errors but none of the PNGs are processed!

If successful, the process should add opaqueWidth and opaqueHeight to the members of the $dataImages object.

Unfortunately, using addLoadListener produces:
dataImages_addLoadListener.PNG

Whilst, the OG code that I had produces:
dataImages_og.PNG

So, you can see a few of the images acquired the opaqueWidth and opaqueHeight properties... but I need all of them to have it. Back to the drawing board for moi.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,480
Reaction score
2,630
First Language
EN
Primarily Uses
RMMZ
Not certain exactly what you tried, but I just tried entering these two lines into the console (F8 during test), from the title screen, and it seemed to work OK for me:
JavaScript:
var myFct = function(bmp) { console.log('loaded\n\t width: ' + bmp.width + '\n\theight: ' + bmp.height); };
var bmp = ImageManager.loadFace('Actor1'); bmp.addLoadListener(myFct);
I picked the face image because that's not something that will be loaded/cached at that point, but it seems to work OK with various other images too (battlebacks, system, enemies, etc). It turned out that bind was unnecessary here since the bitmap gets passed in as the first argument of the callback function. :kaophew:
 

subengari

Veteran
Veteran
Joined
Dec 12, 2020
Messages
36
Reaction score
14
First Language
king's english
Primarily Uses
RMMV
Not certain exactly what you tried, but I just tried entering these two lines into the console (F8 during test), from the title screen, and it seemed to work OK for me:
JavaScript:
var myFct = function(bmp) { console.log('loaded\n\t width: ' + bmp.width + '\n\theight: ' + bmp.height); };
var bmp = ImageManager.loadFace('Actor1'); bmp.addLoadListener(myFct);
I picked the face image because that's not something that will be loaded/cached at that point, but it seems to work OK with various other images too (battlebacks, system, enemies, etc). It turned out that bind was unnecessary here since the bitmap gets passed in as the first argument of the callback function. :kaophew:

thanks @caethyril .

I think I needed to sleep on it... I tried it this morning and it seems to be working better! I appreciate it.


waitForIt_working.PNG

dataImages_addLoadListener_working.PNG

There are two straggling images ("$large_chest_beam_old" and "$large_chest_stars") but they aren't important to what I am doing so it's ok!

Thanks again!
 
Last edited:

Latest Threads

Latest Profile Posts

When you start recoloring your character because lawsuits are expensive:

lawsuits are bad.png
Been busy in Fort Myers and I'm spending tonight and tomorrow in Orlando. Then I go home on Tuesday by train (I take the Amtrak) and rest up before work on Wednesday.
I'm going to make a thread on the forum asking an interesting question... about the next RPG Maker!
#trafficjam #rm2k #catepillar
38bkNHz.gif

Forum statistics

Threads
121,840
Messages
1,144,622
Members
160,100
Latest member
Unknown-Tale
Top