[MZ]Check if map file exist.

Hi guys.
Someone knows what is the code to check if map001(example) exist?
can you go into a few more details of when and why you want to check?

because the only reason I can think of is to check if the file was damaged in the download, otherwise you as the developer will always know which maps are available in your project, no need to check that by code.
can you go into a few more details of when and why you want to check?

because the only reason I can think of is to check if the file was damaged in the download, otherwise you as the developer will always know which maps are available in your project, no need to check that by code.
My game is online and when some player enter in a map that do not exist the server crash.
If i have a code to check is this new map i have created do not existe the player will no teleport.
A map transfer with a variable input should check the map ID before initiating the transfer.
unless you deleted old maps, you can easily do this by checking for the maximum map ID number - every map with a lower ID exists.
A map transfer with a variable input should check the map ID before initiating the transfer.
unless you deleted old maps, you can easily do this by checking for the maximum map ID number - every map with a lower ID exists.
good ideia, thanks bro, but soometimes i delete old maps, so i will try to found this code.
I found to check save file existe, but i want map check.
To find out if map 001 exists, you could normally do something like this:
JavaScript:
$dataMapInfos.some(map => map?.id === 1);

However, that's under normal circumstances, and it sounds like you're doing some highly abnormal stuff, so I dunno whether or not that will help you. If you're just going into your data folder through the file explorer, and adding/deleting stuff all willy-nilly, then it won't work. But if you're properly adding/deleting maps through the RPG Maker editor, then it will work.
To find out if map 001 exists, you could normally do something like this:
JavaScript:
$dataMapInfos.some(map => map?.id === 1);

However, that's under normal circumstances, and it sounds like you're doing some highly abnormal stuff, so I dunno whether or not that will help you. If you're just going into your data folder through the file explorer, and adding/deleting stuff all willy-nilly, then it won't work. But if you're properly adding/deleting maps through the RPG Maker editor, then it will work.
Thanks for your help, but unfortunately dont worked, the hero teleported even the map file not existing.
1663622285530.png

1663623343657.png
Thanks for your help, but unfortunately dont worked, the hero teleported even the map file not existing.
View attachment 240835
View attachment 240836
Like I said before, it won't work if you directly delete the file using your file explorer. That's not the correct way to delete maps, because it still leaves the map listed in the MapInfos file. You need to open the project up in RPG Maker, delete it from the map browser, and then save the project.
Like I said before, it won't work if you directly delete the file using your file explorer. That's not the correct way to delete maps, because it still leaves the map listed in the MapInfos file. You need to open the project up in RPG Maker, delete it from the map browser, and then save the project.
Sorry, I think I didn't explain myself properly.
I don't want to delete maps, what I mean is that sometimes I delete maps by mistake, so doing it that way wouldn't help.
I need code that will make rpgmaker check if map x exists, that's all.
Because if I create a map, here on the server, but the client hasn't downloaded that map yet, then it can't teleport to this new map.
This would avoid the error.
I'm having a hard time envisioning a reasonable scenario where the client will have a MapInfos file that thinks that a map exists, and they will have another map that references that map... yet they won't actually have the map file. That shouldn't even be something that is possible, unless they're manually deleting files.

But regardless, if you insist on checking whether or not the file physically exists on the player's system, here is how you can do it. First, put this code into a new .js file and put in your plugin folder and add it to the project through the plugin manager:

JavaScript:
DataManager.localDataFileExists = function(filename) {
    const path = require("path");
    const base = path.dirname(process.mainModule.filename);
    let dataPath = path.join(base, "data/");

    const fs = require("fs");
    return fs.existsSync(dataPath + filename + ".json");
}

Then you can use this script call to check if it exists:
JavaScript:
DataManager.localDataFileExists('Map216');

Note that this will only work for PC/Mac/Linux. It will not work for mobile and web builds.
I'm having a hard time envisioning a reasonable scenario where the client will have a MapInfos file that thinks that a map exists, and they will have another map that references that map... yet they won't actually have the map file. That shouldn't even be something that is possible, unless they're manually deleting files.

But regardless, if you insist on checking whether or not the file physically exists on the player's system, here is how you can do it. First, put this code into a new .js file and put in your plugin folder and add it to the project through the plugin manager:

JavaScript:
DataManager.localDataFileExists = function(filename) {
    const path = require("path");
    const base = path.dirname(process.mainModule.filename);
    let dataPath = path.join(base, "data/");

    const fs = require("fs");
    return fs.existsSync(dataPath + filename + ".json");
}

Then you can use this script call to check if it exists:
JavaScript:
DataManager.localDataFileExists('Map216');

Note that this will only work for PC/Mac/Linux. It will not work for mobile and web builds.
Worked bro, thank u so much 🙂
That worked? I thought you said your game was online? Like @Arthran said, that code won't work on browser / mobile builds. For browser builds, you could use that first method

JavaScript:
$dataMapInfos.some(map => map?.id === 1);

But with a twist (it's not a great solution if you have a lot of maps, but then again, I don't think a great solution exists for the problem you described unless we're missing something). When the game loads you could try to pre-load all the maps in MapInfos and immediately exclude the ones that fail.

This tiny little plugin will try to pre-load all the maps in MapInfos.json and for those that fail to load, they will be removed from MapInfos (or rather replaced with `null` since that's the default behaviour when you delete maps via the editor).

With this plugin enabled, that first snippet will work when checking for the existence of a map file.
I recommend you to open the dataMapInfos.json in a text editor and manually set maps to "null" when they have been accidentally deleted.

Original thread: https://forums.rpgmakerweb.com/threads/mz-check-if-map-file-exist.151404/