MV How to wait for Bitmaps to load?

How do you wait for Bitmaps to finish loading?

I know this isn't the best scenario. Ideally you would tell the ImageManager to load images way in advance, but this is just simply not always possible. Sometimes you need to load images on the fly. The problem is of course that images don't load instantaneously. So the question now is, how do you wait for them to finish loading or rather, how do you know a Bitmap has finished loading?

The obvious things to try would be the functions
JavaScript:
Bitmap.isReady()
and
JavaScript:
ImageManager.isReady()
but they don't work. Both return 'true' despite the Bitmap very clearly not being ready.

For transparency, here is how I determine that the two 'isReady()' functions don't communicate that a Bitmap is ready:

JavaScript:
let messageImageBitmap = ImageManager.loadPicture('redRectangle');
while((!ImageManager.isReady) || (!messageImageBitmap.isReady)){
    console.log('Image not ready')
}
console.log(messageImageBitmap.height);

'Image not ready' is never printed to the console and messageImageBitmap.height returns 0 despite 'redRectangle.png' being a 300 x 300 image file.

By the way, I know that is horrible blocking code that I would never use in that way, it was just a quick and dirty way of testing if Bitmap.isReady() or ImageManager.isReady() do what I need them to do.

So with that the question remains, how do I check that a certain Bitmap has finished loading and is now fully ready for usage? Please someone help me, I am going mad over this.
Hi!

You can use the addLoadListener method:

JavaScript:
let messageImageBitmap = ImageManager.loadPicture('redRectangle');
messageImageBitmap.addLoadListener(callBack)
JavaScript:
let messageImageBitmap = ImageManager.loadPicture('redRectangle');
while((!ImageManager.isReady) || (!messageImageBitmap.isReady)){
    console.log('Image not ready')
}
console.log(messageImageBitmap.height);
The problem with this is that you're checking the existence of the functions, rather than evaluating those functions and checking their result, e.g.
JavaScript:
let bmp = ImageManager.loadPicture('redRectangle');
while (!bmp.isReady()) console.warn('not ready yet');
console.log('bitmap height: ' + bmp.height);
However, like Eliaquim mentions, listeners are a better option!

Also, if you are using a lot of images in your game, consider raising the cache size, e.g. using the Community_Basic plugin present by default in new projects.
You can use the addLoadListener method:
Yes, thank you very much. After wrangling a little bit with dynamically generating functions, that worked just fine.

The problem with this is that you're checking the existence of the functions
What an embarrassing mistake to make :| I am not quite used to VS Code and Javascript. Coming from Java that would have been an obvious syntax error that the IDE would have caught, but in Javascript that is of course valid syntax and a useful feature.

Also, if you are using a lot of images in your game, consider raising the cache size, e.g. using the Community_Basic plugin present by default in new projects.
I was already thinking about this. Is there a somewhere I can read up on the games cache? Does the cache size represent how much VRAM the game will allocate? By how much can I / should I raise it? Will I run into problems if I raise it to much? Is raising the cache as simple as just increasing
JavaScript:
ImageCache.limit
?
I was already thinking about this. Is there a somewhere I can read up on the games cache? Does the cache size represent how much VRAM the game will allocate? By how much can I / should I raise it? Will I run into problems if I raise it to much? Is raising the cache as simple as just increasing
JavaScript:
ImageCache.limit
JavaScript's garbage collection clears data from memory when no more references exist to that data:
RMMV's image cache simply keeps recent images "alive" by storing a reference to their data. If the game tries to load an image, it checks the cache first:
  • If it finds a reference to the requested image, it can simply return that reference rather than loading the data again from file.

  • If it doesn't find a reference, the data is loaded from file into a new, appropriately-labelled cache entry. If this takes the cache size over the limit then cache entries are deleted, starting with the last-referenced ("oldest"), until the size is below the limit again.
The limit exists to keep the game's RAM consumption at or below a known value. For example, you don't want some intro cutscene images occupying RAM until the end of the play session! This is particularly relevant for image-heavy games on devices with relatively low RAM (e.g. mobile).

Remember that image data in RAM will be raw/bitmap format, which may be several times larger than the size of the corresponding PNG file.

Yes, increasing ImageCache.limit should be sufficient; that's what the Community_Basic plugin does~

Original thread: https://forums.rpgmakerweb.com/threads/how-to-wait-for-bitmaps-to-load.142431/