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?
😊]