MZ How to get the filesystem path in web distribution?

Hi all,

I recently released an Achievement plugin that needs to write info on a JSON. Everything is great until the game is distribuited on PC but I got report from a dev that it crashes if web deploy is used. I tried to look into it and this is where it throw the error:


JavaScript:
        const fs = require('fs');
        const path = require('path')

On PC it works, on web deply it says "require is not defined".
Sadly I'm not really skilled in the web deployement, does the RMMZ define another function for that?
Of course, a web browser cannot access our local filesystem, otherwise, my next game will be "click here and your hard disk is nuked!"

In more detail, "require" is simply not supported in a browser game. It's a NodeJS command: https://nodejs.org/api/modules.html#requireid and a browser game starts without NodeJS.

What you want to do instead is to use the StorageManager class in rmmz_managers.js
Of course, a web browser cannot access our local filesystem, otherwise, my next game will be "click here and your hard disk is nuked!"

In more detail, "require" is simply not supported in a browser game. It's a NodeJS command: https://nodejs.org/api/modules.html#requireid and a browser game starts without NodeJS.

What you want to do instead is to use the StorageManager class in rmmz_managers.js

Thanks, that's a great help! I'll look into it
Specifically:
  • Save: StorageManager.saveObject(filename, data); and
  • Load: StorageManager.loadObject(filename).
Where:
  • filename is a string representing the name of the file, without extension.
  • data is a JSON-serialisable object, e.g. an Object or Array.
These work for standalone- or web-deployment. MV/Z web games save to the local storage of the user's browser, much like cookies.

Edit: also, note that these are asynchronous calls that return a Promise. You can follow up as usual with then (success), catch (fail), and/or finally (any).

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-the-filesystem-path-in-web-distribution.164256/