Howto: Check if image exists?

Status
Not open for further replies.

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
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,640
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
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
Thanks, it works now!
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,862
Reaction score
5,240
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.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.
time for a new avatar :)

Forum statistics

Threads
106,017
Messages
1,018,356
Members
137,802
Latest member
rencarbali
Top