MV LoadPicture, not showing initial load

Working in pure JS on this; I have a script that runs a custom scene. I build a scene with a background and some menus, giving a sort of visual novel look.
On initial load of the scene, the background image is loaded, and we loop through the NPCs and load an image for each of them. Then setup the menus.
Right now, the background image loads perfectly fine. We're golden. As we navigate the "rooms", each subsequent background image is loaded fine.
The NPC images, however, have a hangup. On the initial load, they don't show. On a subsequent load, or when one of the scripts "reload" the room, they do. If the same image is referenced elsewhere, it shows, but new "npc" images take that first round off.
The background and NPC elements are created as new Sprites, sans a provided file. As each of the loads occurs the bitmap is set to an image loaded via ImageManager.loadPicture.
A later pair of elements are loaded as Sprite_Buttons, which show and behave perfectly fine.

I've tested a few things out; disabled the background image, so we're not creating/loading that, and same result. Loaded only one NPC/cut the loop, same result. The image files themselves, the background images have a larger file size than the NPC images, so it shouldn't be a cache/memory issue.
Any ideas?
The bitmaps haven't fully loaded by the time you are drawing them.

Three main approaches:
  • Just load your image files earlier, e.g. run ImageManager.loadPicture() when you create your scene.
  • You can use bitmap.addLoadListener() to run a callback function after a specific bitmap loads. If you are waiting on multiple bitmaps you'll likely need to track all of them and then redraw your window elements once all of the bitmaps have loaded. You can find usage examples by searching the forums.
  • Just set the images up as sprites. This can have numerous benefits:
    • Sprites can self-update independently of the window/scene.
    • You only need to draw them once unlike drawing bitmaps directly to a window with blt() where they generally will get redrawn with each refresh().
    • You can potentially reuse sprites just by changing the sprite's bitmap, among other benefits.
I thought so too, but the room backgrounds are larger images.
I did some more testing this morning and I think I nailed it. I initially swapped the call, so the background loaded the NPC graphic and the "NPC" loaded the background. If its an issue related to the loading, I wagered the background loads fine and fast, it should appear (and block the NPC from showing up). When it booted up the NPC graphic, loaded to the background slot, appeared right away...the room graphic did not.
The ONLY difference between the two elements is the background image is loaded and set; the NPCs get an update to their x and y coordinates. THAT is what is causing the visibility issue. I disabled setting the NPC coordinates and they load in at (0,0) right away.
Ultimately I'm missing an update to the NPCs sprite or something similar to inform the game its "moved".

EDIT: I forget etiquette sometimes around double posts, but caught myself when I figured I'd share the code involved.

JavaScript:
Scene_MysteryInvestigation.prototype.updateNPCs = function() {
    const room = this._caseData.rooms.find(r => r.name === this._currentRoom);

    for(i=1;i<this._npcSprites.length;i++) {
        this._npcSprites[i].bitmap = null;
    }

    var totalWidth = 0; var i = 0; var npcSprites = [];
    if (room && room.npcs.length > 0) {
        room.npcs.forEach(name => {
            const npc = this._caseData.suspects.find(s => s.name === name);

            npcSprites[i] = ImageManager.loadPicture("npcs/" + this._caseData.location + "/" + npc.gender.toLowerCase()); console.log("Loading " + npc.gender.toLowerCase() + ".png");
            totalWidth += npcSprites[i].width;

            i++;
        });

        var initX = 408-(totalWidth/2);
        for(i=0;i<npcSprites.length;i++) {
            var x = initX; initX += npcSprites[i].width;
            var y = 624-(npcSprites[i].height);

            this._npcSprites[i].bitmap = npcSprites[i];
            this._npcSprites[i].move(x,y);
        }
    }

}

So here's the relevant code; when a new "room" is loaded, this is called to update the NPCs.
The _npcSprites are defined at the start as new Sprites without an image.
On a room load, we first blank the _npcSprites. We then loop through the room data for NPCs who are present; at testing I have a Male and Female sprite, so we grab the gender of the NPC for our file name and assign it to npcSprites (no underscore). The loop deals with if there are more than one NPC and builds the new set of images and gets the total width of the group.
Following up, we get an initial X position so we can center the group, loop through the newly loaded NPCs images and assign them to a _npcSprite, set the coordinates.

Now, if I DON'T set the coordinates, the sprite loads as intended at (0,0). If I DO set the coordinates, we don't see the sprite until a subsequent reload where that particular image is concerned. IE if we move to a new room, where the same _npcSprite is being used but its the female graphic, not the male, it too won't show. Pop back to the first room with the male? Male shows. Pop BACK to that second room with the female? It too shows.

EDIT THE THIRD: Tried a few more tests. It IS a load issue; since a couple calculations are concerned about the specs of the image, that is why the NPC positioning fails while the Background does not. The width and height are loading as 0s, putting the calculations out of whack.

AND FINAL EDIT, and solution for my use case 🙂
I pulled the positioning code from the updateNPCs() function and wrote a new placeNPCs() function. I added a true/false "_npcLoaded" flag which is set to false at the end of the update function. In placeNPCs() we check the flag for true, and return (do nothing) if so. The following script attempts to tally a totalWidth, and then place the sprites. If the totalWidth is greater than zero, the files are viewed as loaded and the _npcLoaded flag is set to true. Tossed a call to that into my scene's update function, and voila~
If you are trying to calculate something off of the dimensions of a bitmap, that info (e.g. sprite.bitmap.width) isn't available until the bitmap has fully loaded, and attaching .addLoadListener to the bitmap would be a good way to do something with those numbers when they do populate.

Sounds like you worked around it though, so congrats on figuring it out.

Original thread: https://forums.rpgmakerweb.com/threads/loadpicture-not-showing-initial-load.178947/