MZ Check the existence of image file [Solved]

Greetings, I tried to make a system where it will show a picture based on the current mapID and also don't show the picture if the there's no picture file based on a said mapID exist. Here's the code.


JavaScript:
var MapDisplay = MapDisplay || {};

MapDisplay.showMap = function() {

    // Retrieve the current map's ID.
    const mapId = $gameMap.mapId();
    // Retrieve map images path
    const imgPath = "Maps/";
    //Combine the path and name file
    const fullPath = imgPath + mapId;
    const MapID_pngformat = mapId+".png";
    

    

    // Set up the parameters for displaying the picture
    const pictureId = 10;       
    const origin = 1;         
    const x = 512;             
    const y = 256;             
    const scaleX = 100;       
    const scaleY = 100;       
    const opacity = 255;       
    const blendMode = 0;       

    


pictureExists = function(MapID_pngformat) {
    const fs = require ("fs");
    return fs.existsSync("./img/pictures/Maps" + MapID_pngformat);
};




    if(pictureExists){
        //If the image file exist then show it.
    $gameScreen.showPicture(pictureId, fullPath, origin, x, y, scaleX, scaleY, opacity, blendMode);
    }else{
        //Otherwise call a common event
        $gameTemp.reserveCommonEvent(159);
    }



};

My biggest struggle is with how to actually detect the said image files. I've been scouring through the forum and so far, there's no good and consistent solution for RPG Maker MZ.

Thanks in advance!
Your pictureExists function looks fine, although you should really add a const, let or var keyword when declaring it. However in your conditional statement you're not calling the function, it should have closing parenthesis after the name, like so.


JavaScript:
if(pictureExists(/*mapId*/)){
     //If the image file exist then show it.
}}

another thing to take not on is that this will only work for desktop builds of your game, this will not work for browser deployments.
Your pictureExists function looks fine, although you should really add a const, let or var keyword when declaring it. However in your conditional statement you're not calling the function, it should have closing parenthesis after the name, like so.


JavaScript:
if(pictureExists(/*mapId*/)){
     //If the image file exist then show it.
}}

another thing to take not on is that this will only work for desktop builds of your game, this will not work for browser deployments.
After some tweaking based on your feedback, the code is working like a charm. Thank you so much.

I'll just share the code in case anyone have encountered this issue.

JavaScript:
MapDisplay.showMap = function() {

    let IsThereAMapPicture;

    // Retrieve the current map's ID.
    const mapId = $gameMap.mapId();
    // Retrieve map images path
    const imgPath = "Maps/";
    //Combine the path and name file
    const fullPath = imgPath + mapId;
    const MapID_pngformat = mapId+".png";
   

   

    // Set up the parameters for displaying the picture
    const pictureId = 10;      
    const origin = 1;        
    const x = 512;            
    const y = 256;            
    const scaleX = 100;      
    const scaleY = 100;      
    const opacity = 255;      
    const blendMode = 0;      

   


let pictureExists = function(MapID_pngformat) {
    const fs = require ("fs");
    return fs.existsSync("./img/pictures/Maps/" + MapID_pngformat);
};

    if(pictureExists(MapID_pngformat)){
        //If the image file exist then show it.
    $gameScreen.showPicture(pictureId, fullPath, origin, x, y, scaleX, scaleY, opacity, blendMode);
    }else{
        //Otherwise call a common event
        $gameTemp.reserveCommonEvent(159);
    }

};

another thing to take not on is that this will only work for desktop builds of your game, this will not work for browser deployments.
If you don't mind. How one should approach it for browser build? And is this method works for like mobile build?

Once again, thanks.
For browser builds, I think you'd need to use XMLHttpRequest and handle a 404 response. This also means you wouldn't be able to do the check synchronously like you're doing in your code.
JavaScript:
function UrlExists(url) {
  var http = new XMLHttpRequest();
  try{
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
  }catch(ex){return false;}
}

This is the code I'm using. It just works but sync option for XMLHttpRequest may freeze the game if the server is slow in response.

Another method is building a complete file list after you deploy the game, and check if the file exists.
JavaScript:
function UrlExists(url) {
  var http = new XMLHttpRequest();
  try{
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
  }catch(ex){return false;}
}

This is the code I'm using. It just works but sync option for XMLHttpRequest may freeze the game if the server is slow in response.

Another method is building a complete file list after you deploy the game, and check if the file exists.
Thanks for your reply. This might come in handy in the future.

Original thread: https://forums.rpgmakerweb.com/threads/check-the-existence-of-image-file-solved.178073/