Check for image existence / Replace with placeholder

Why hello there,


it's me with another quick question.


How can I check if an image exists? (for example in my "img/system" folder)


Situation:


Thing is, I want to display a different icon for each of my items (the small icons from the database are more thought of as categories), so each weapon should have its own icon additionally to the small database icon.


Since I'm using Yanfly's ItemCore, I edited the


Window_ItemStatus.prototype.drawLargeIcon()


function. (~line 1600)


The image name should be the item name (with spaces replaced by underscores)


So far so good, I managed to do all of this.


But if there isn't an image with that name (of the item) it should just display a placeholder image.


Window_ItemStatus.prototype.drawLargeIcon = function() {
var iconBigName = this._item.name.replace(/ /g, "_");
var bitmap = ImageManager.loadSystem(iconBigName);

if (bitmap.exists()) { //THIS IS THE LINE I'M STUCK WITH

this.contents.blt(bitmap, 0, 0, 64, 64, 0, 0, 128, 128);
} else {
this.contents.blt("placeholder", 0, 0, 64, 64, 0, 0, 128, 128);
}
};


I messed around with different requests, also with ImageManager._cache, but my js knowledge is very basic and I couldn't figure out how to edit the code so it'll work.


I think the problem is that it tries to load the bitmap, even if it doesn't exist. But I just don't exactly know how to request its existence beforehand 🙁  That's all i figured out 😀  


Anywho, thanks for your help!


Simon
*bump*


Only bumping this because I think it's a minor problem to solve.


If someone could provide the code I'm missing to fix this, this thread could be closed. Ty <3
Check the forum rules - you can only bump after 3 days.


The issue with ImageManager is that it loads images asynchronously.  So you won't know whether the bitmap exists or not on the line immediately after ImageManager.loadSystem.  Even if it did exist, I'm not sure you could blit it to another image straight away, for the same reason.


Take a look at DataManager and StorageManager to see how they check if a file exists.  You could probably do something along the same lines, though it will require more than a single line in an if test.  Check if image exists.  If it does, do the call to ImageManager and put the result into this.contents rather than blitting it afterwards (if you can).  If the file doesn't exist, then you can use the placeholder.
Oh, sorry about the bumb then  🙁


Anywho, I think I got is somehow. I changed the drawLargeIcon() to this:


Window_ItemStatus.prototype.drawLargeIcon = function() {
//Set image name as variable
var iconBigName = this._item.name.replace(/ /g, "_");
var iconBigName = iconBigName.toLowerCase();
var bitmap;

//Get relative image path (similar to StorageManager)
var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, '/img/system/icons/'+iconBigName+'.png');
if (path.match(/^\/([A-Z]\🙂/)) {
path = path.slice(1);
}
path = decodeURIComponent(path);

//Load file system to check for image availability
var fs = require('fs');
if (fs.existsSync(path)) {
bitmap = ImageManager.loadSystem('icons/'+iconBigName);
} else {
bitmap = ImageManager.loadSystem('icons/placeholder');
}

//Disable smoothing, draw image, enable smoothing again
this.contents._context.imageSmoothingEnabled = false;
this.contents.blt(bitmap, 0, 0, 64, 64, 0, 0, 128, 128);
this.contents._context.imageSmoothingEnabled = true;


Now, that works just fine, but there is still one bug left


If I scroll through the items, the large icon only loads after I "hovered" over 2 items (so on the third item)


or


if I open the inventory, close it and open it for the second time.


(see attached gif of me going through the menu)


Now - I am aware that I changed up the Item Windows and so on, but it all works perfectly normal with yanflys default drawLargeIcon() function.


So if that's got anything to do with what I put together here, please respond here.


Else I'll make another, seperate thread!


Thanks


Simon


screen.gif
You can use a callback function to draw the image onto the window as soon as its loaded


Ex:

Code:
    var picture = ImageManager.loadPicture(this._item.image);
    var window = this;
    picture.addLoadListener(function () {
        window.contents.blt(picture, 0, 0, picture.width, picture.height,
                0, 0);
    });
Thank you very much. Works just fine.


Can be closed.

Original thread: https://forums.rpgmakerweb.com/threads/check-for-image-existence-replace-with-placeholder.66622/