Howto: Check if image exists?

Status
Not open for further replies.

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
I have an image here: <project dir>/img/minimap/005.png
Code:
SE.TEST = SE.TEST || Utils.isOptionValid('test');
    SE.Minimap.minimapFolderPath = function()
    {
        var suffixPath = '/img/minimap/';
        if (!SE.TEST) { suffixPath = '/www' + suffixPath; }
        var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, suffixPath);
        if (path.match(/^\/([A-Z]\:)/)) { path = path.slice(1); }
        path = decodeURIComponent(path);
        return path;
    }

    SE.Minimap.getFullMinimapPath = function(filename)
    {
        return SE.Minimap.minimapFolderPath() + filename;
    }

    // filename example: '002.png'
    SE.Minimap.minimapImageExists = function(filename)
    {
        var fs = require('fs');
        console.log(SE.Minimap.getFullMinimapPath(filename));
        return fs.existsSync(SE.Minimap.getFullMinimapPath(filename)); // <<<<<< always returns false.
    }
Sadly this function always returns false.
Console output: /img/minimap/005.png

Code to load image:
Code:
ImageManager.loadMinimap = function(filename, hue)
{
    return this.loadBitmap('img/minimap/', filename, hue, false);
};
this.mapBmp = ImageManager.loadMinimap(this.mapName);

Is this because I launch RPGMaker through Steam now? Does it change the executable path or something for the ImageManager? This used to work in the older version of RMMV when used with the standalone editor (=not Steam).

The code ImageManager.loadBitmap() always returns a BitMap, even if it does not exist on disk so I can't just do a null-check.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,644
First Language
Czech
Primarily Uses
RMMV
There are multiple issues.
1. decodeURIComponent... I don't see the use as justified. You don't have encoded filepath, so why decode it? All it does is change %20 etc. back to spaces.
2. window.location.pathname is not the way to go. NWJS of newer MVs is built to encrypt the filepath just to make it safer in case you need to share screenshots. So "isagjsadoigjsdgjhiojsgksfgasfksafgsk/img/minimap/002.png" is not a valid filepath.

Here's your fixed code. I also striped it of all unnecessary junk.
Code:
SE.Minimap.minimapImageExists = function(filename) {
    var fs = require ("fs");
    return fs.existsSync("./img/minimap/" + filename);
}
Note that the "./" was the way to go, it's the home folder. In C++ you must input the absolute path, not the relative path.
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
Thanks, it works now!
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
8,029
Reaction score
5,332
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

I remember once when I was at Disney's Animal Kingdom and I got to dance with some of the cast members! It was so much fun! After the show my dad and I got frozen lemonade and some ice cream. It makes me feel better remembering the good things about my dad... maybe I will return to Orlando.
spending almost entire day to mod skyrim but it still couldn't run properly :(
My friend has become one with the glitter. @ImaginaryVillain, got any room in the Cult of the Sporkle?
I still cannot wrap my head around how to edit and use arrays in RPG Maker MV o_O
Gamemaking has been slower, but I do my best to make small progress every day. :)

Forum statistics

Threads
107,714
Messages
1,031,616
Members
139,854
Latest member
SenTony
Top