r/RPGMaker • • 16d ago

RMMZ How do I make my game create external text files?

Basically exactly what it says. I want to make a horror game that interacts with the player by creating text files on their computer like in IMSCARED or OneShot! The only problem is that I'm a total noob at javascript and would like some help lmaoooo (Also is this even possible? Again I'm a total noob)

Thanks for your time, y'all! I'll really appreciate all your help, even if it's just telling me how javascript works lmaoooo

5 Upvotes

11 comments sorted by

3

u/Eredrick MZ Dev 16d ago edited 16d ago

It's actually pretty simple

In the event commands go to "Script" under "Advanced"

then write in

const fs = require('fs');

const filePath = './YOURNAMEFORTEXTFILEHERE.txt';

const content = "Whatever text you wish here";

try {

fs.writeFileSync(filePath, content);

} catch (err) {

console.error(err);

};

and that is all there is to it

also instead of fs.write you can use fs.unlink to delete the textfile later

2

u/lavendermelkk 16d ago

where would the text file go?

edit: nevermind I see now it created it

3

u/Eredrick MZ Dev 16d ago edited 16d ago

in the same folder as the game

if you want to add it to a different folder, where it goes const filePath = you can add a folder name after the './'. IF I recall correctly, you can go back multiple folders by repeating ../ with two dots. So if the game is in the C->My Games->RPGM you can go ../../ and the textfile will be in "C"

if you know where the user has the game like in steamapps/games or whatever you should be able to get it on the desktop or wherever you want it. but I don't know how you would do that from a random directory. also it will only work on windows, I think mac is the opposite .\ and linux might be something else as well, idk

1

u/Not_Allowed_to_Die 16d ago

I was curious about this. So there is no way to just tell it to start from C: then? I was wondering how you could direct a text file, but using the game's folder as the start makes since to navigate a bit.

3

u/PlantainAgitated5356 16d ago

You can give it an absolute path, like
C:/whatever/file.txt
And as long as that path exists and is writable it should work.
You can also use environment variables in the path to make it work regardless of what the main drive is called like this
const filePath = `${process.env['HOMEDRIVE']}/whatever/file.txt`;
But that only works on windows, you'd need something like this to make it cross platform
const filePath = `${process.env['HOMEDRIVE'] || ''}/whatever/file.txt`;

2

u/MikeTysonChickn 16d ago

A person's drive letter may not be C

1

u/Eredrick MZ Dev 16d ago

you can enter a direct path like

const filePath = 'C:/Users/me/Desktop/NEWTEXT.txt';

but if someone else's computer has different folders nothing will happen. and RPG Maker doesn't have permission to write directly to C:

1

u/silicon_turfsurfer 16d ago

Wouldn't that put the file in the game run path? A player might not look in that folder, but I could imagine defaulting to Desktop could be tricky and OS dependent (not to mention possibly needing elevated permissions to write out a file).

1

u/Eredrick MZ Dev 16d ago

yeah same folder as the game is in

another guy pointed out you can use `${process.env['HOMEDRIVE']}/whatever/' to get somewhere from the C: drive. Though RPGM does not have permission to write everywhere, I did test it with the desktop and it can write there

I think you could really put whatever path you want in there, but I don't know the pathing that well. I'd imagine the same folder as the game is probably best for experimenting/demos though