In MZ can you still use 'File.Exists?' as a conditional branch to see if a file is present like in VX Ace?

After a picture is shown I'd like to check to see if another picture exists or whether there are no more pictures to show the player.

In VX Ace I can use File.exists?($game_variables[x]) to see whether a file exists on my computer so I know this was something the engine could do. I tried File.exists?($gameVariables.value(x)) in MZ and was unsucessful. If anyone knows the correct function to check whether a file exists in RPG Maker MZ please let me know!

Thank you!
As a general rule, you can't just use syntax from a different programming language and hope it might work 😛

I Googled "javascript file exists" and got that it doesn't have this functionality as a part of the default language.

So possible solutions are:
- Load the file with the included functions per the file type, then check whether the result is null

- Use different logic. You are giving the player all of the files that can be in their game, how can you not know ahead of time whether the file exists?
It's slightly complicated by the fact that MZ supports both local and web deployments, which use different file systems. Web games store data in the browser's storage (similar to cookies) rather than as separate files. For security reasons, web games can't check the local file system.

To check if a save file exists in MZ, you can use something like this in a Script command:
JavaScript:
const varId = 1;   // game variable to output result
const saveId = 1;  // save slot ID
const saveName = DataManager.makeSaveName(saveId);
StorageManager.loadObject(saveName)
              .then(() => $gameVariables.setValue(varId, 1))
              .catch(() => $gameVariables.setValue(varId, -1));
This will set the value of the specified variable to 1 if the file is found, or -1 if there was an error attempting to load (usually because the file does not exist).

It works asynchronously! If you want to use the result immediately, you can set up a loop, e.g.
Spoiler: example event setup
◆Control Variables:#0001 FileCheck = 0 ◆Script:const varId = 1; // game variable to output result : :const saveId = 1; // save slot ID : :const saveName = DataManager.makeSaveName(saveId); : :StorageManager.loadObject(saveName) : : .then(() => $gameVariables.setValue(varId, 1)) : : .catch(() => $gameVariables.setValue(varId, -1)); ◆Loop ◆If:FileCheck = 0 ◆Comment:Not done yet, check again next frame. ◆Wait:1 frame ◆ :Else ◆Break Loop ◆ :End ◆ :Repeat Above ◆If:FileCheck = 1 ◆Text:None, None, Window, Bottom : :File found! ◆ :Else ◆Text:None, None, Window, Bottom : :File not found. ◆ :End
That's also why I used a variable instead of a switch, for the "undetermined" state.

[Edit: I think it's also possible to use await StorageManager.loadObject etc to wait for it, but I wouldn't recommend that because it breaks away from the core engine, meaning you wouldn't be able to know in advance when the result is going to be processed. 😴]

[Edit 2: I forgot...for save files there's actually an exists function:
JavaScript:
DataManager.savefileExists(1) // does save ID 1 exist?
😊]
For context, I'm on a PC build (not web/mobile) and I don't need release compatibility, I only care if it works locally so I can play it.

I have a magazine type of common event where I can pageup/pagedown to show the next page in the magazine. In VX this works/worked by checking to see if there is an additional picture file in the specific magazine's folder. If so, I can hit pagedown and it'll show the next page, otherwise it knows I am at the end of the magazine and the 'next page' option is not available.

The filecheck is used so that despite a magazine having 3 pages, 11 pages, 48 pages, 6001 pages, or any number whatsoever the common event knows exactly if there is another picture to show or not.

So to be clear, am I unable to do this in MZ altogether, or would I require a plugin or something? The more I work with MZ its kinda letting me down. There are more and more things VXAce could do effortlessly that are huge obstacles in MZ.

Any help is appreciated!
Is there some reason you can't just change a single value in your event for this? 😅

Otherwise, I tested this for checking map files and it seems to work for me:
JavaScript:
const dir = "data";
const mapId = 12;
const filename = `Map${String(mapId).padZero(3)}.json`;
const P = require("path");
const path = P.join(P.dirname(process.mainModule.filename), dir, filename);
alert(`File "${path}" ${require("fs").existsSync(path) ? "found" : "not found"}!`);
Or, abbreviated for Conditional Branch -> Script:

const dir = "data", mapId = 12, filename = `Map${String(mapId).padZero(3)}.json`, P = require("path"); require("fs").existsSync(P.join(P.dirname(process.mainModule.filename), dir, filename))
In your case you'd want to edit the dir and filename values, and either remove or repurpose mapId.

(The require parts won't work for web games, but you said that's OK.)
Is there some reason you can't just change a single value in your event for this? 😅

Otherwise, I tested this for checking map files and it seems to work for me:
JavaScript:
const dir = "data";
const mapId = 12;
const filename = `Map${String(mapId).padZero(3)}.json`;
const P = require("path");
const path = P.join(P.dirname(process.mainModule.filename), dir, filename);
alert(`File "${path}" ${require("fs").existsSync(path) ? "found" : "not found"}!`);
Or, abbreviated for Conditional Branch -> Script:

const dir = "data", mapId = 12, filename = `Map${String(mapId).padZero(3)}.json`, P = require("path"); require("fs").existsSync(P.join(P.dirname(process.mainModule.filename), dir, filename))

In your case you'd want to edit the dir and filename values, and either remove or repurpose mapId.

(The require parts won't work for web games, but you said that's OK.)
This is exactly what I was missing, It allows me to do exactly what I wanted. 🙂
Directory changing works fine and png is an accepted filetype.

By the way, since it was mentioned that MZ was not meant to check files outside of the RPG Maker directory I didn't bother trying to circumvent that limitation though scripting, but I did confirm that it does have the capability to check outside the directory (by dropping a folder that is symbolic linked elsewhere into the MZ directory) because I wanted to see whether this was a hard limitation or not. (It isn't).
And the reason I didn't simply use a variable to track page amount is that I would need to update the variable for each magazine, and again anytime I modify the amount of pages a magazine has. Meaning if I add a pic I'd have to increase the variable. If I remove a pic I'd have to decrease it, and if I add a bunch of pics I'd have to check the total amount of pictures in the folder and update the variable accordingly. And that needs to be done for any picture addition/removal for EACH magazine/book. If I were to miscalculate at any point it would result in the common event not working exactly as intended. However, with the ability to check whether a next page exists via script instead I can now update the magazines dynamically and they simply work. I can have a folder called Spider-Man for example and anytime I find a cool spider-man picture online I just save it to my spider-man folder. In-game there is nothing for me to change or update, when the 'Spider-man' magazine/comic/book/whatever is called via common event it just shows all the pictures with no hassle.

Thank you again for the help everyone!

Original thread: https://forums.rpgmakerweb.com/threads/in-mz-can-you-still-use-file-exists-as-a-conditional-branch-to-see-if-a-file-is-present-like-in-vx-ace.160689/