RPGMakerMV & Node.js Part 1: Reading And Writing Files

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
Introduction
Following up on the Node.js post, we're going to create our first plugin in RPGMakerMV with Node.js. Now, this post will require some JavaScript knowledge to follow effectively. Let's begin.

Part 1: Setting Up Our Plugin
First, we create our closure. A closure is a function wrapped in a function expression. This allows our code to be protected and execute immediately in a safe environment without conflicting with other people's plugins. Here is what you should have:


In this stage, we set up our anonymous function with another function "setup" inside. I personally create this function to split up my code further -- if necessary. This allows us to keep our plugin in multiple parts. Now, the next step.

Part 2: Declaring Our FS Class
FS stands for the file system; we're going to create a class to handle the file system. In this case, this class is going to read and write files. Let's start with declaring our class. Here's our declaration:


So, now we've declared the important part of our class. We declared the class, plus the writeFile, readFile, and createPath methods. Notice the parameters in readFile and writeFile; they take the file path and file name as two different parameters, because of createPath. This is because of how NW.js handles the directory structure, which we'll see later. Now, let's write our implementation, which is the Node.js part of this instruction.



Part 3: Defining Our Methods -- Node.js Included
Now, we define our class and take advantage of Node.js module "fs". "fs" is the file system module, which allows us to read and write files, plus open directories. To use "fs" we have to use the require keyword. Here's how we define it all in our class:


Okay, there's a lot to unpack here; let's begin. Here we use require to get the Node.js module "fs". We then implement our createPath method to check if NW.js (what MV is built on) is in test mode, then work out our path. Then we use the createPath method within writeFile and readFile to create the filePath. Then we use writeFileSync and readFileSync from "fs" within our custom class to read/write files in any form combining the filePath with the filename to access the correct location. Finally, we export it on line 53, providing access in our RPGMakerMV game. Here's an example output:



Event Setup



In-game Example



Console Log



File Within Directory



File Output



Part 4: How It Works On The System
When we run the Node.js functions we are using system calls on our computer to complete these write and read operations. System calls are the real building blocks of the operating system; they are it's most basic functions. Node.js uses these to perform the operations we want with a higher level API. This is the most basic concept for the "fs" module in Node.js. However, there are other modules that perform other awesome tasks. Tune in for those next time!

Hopefully, you learned something. This is just my interpretation. Now, if anyone has other interpretations of how it works, please share in the comments. Also, if you want to download the tutorial file, you can and use it as a plugin! Happy game making everyone!
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,600
Reaction score
2,359
First Language
French
Primarily Uses
Hum this not bad but MV already use their own plugin Loading xD
so require is not kinda necessary on SOME part not all sometime you need to use the require call lol

EDIT : nvm I saw what you mean it's good it's although not cross platform :/ as I remember require doesn't work for Mac?
I think it's doesn't work locally...meh unsure
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
It should work on most systems/PCs; the "require" functionality should be a part of NW.js. NW.js packages its own version of Node.js. BUT it definitely won't work on browsers, Android, or iOS because they don't have Node.js as a part of it.
 
Last edited:

Kyuukon

主人公
Veteran
Joined
Aug 22, 2013
Messages
2,217
Reaction score
1,076
First Language
Spanish
Primarily Uses
RMMV
I used something like this to make a .txt that indicates whether the player has done X route. After he has done all routes, the true ending appears, regardless of the save file!
Question: it's not possible to reach %AppData% folders with JS, right? Or how'd I do that?

Emh...

I wantz that BGM playing window plugin xD!!
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
The App data folder should be reachable, but you'd need to use the path module in Node.js. I don't have the code for it right now, but those % % symbols would be replaced with spaces most likely.

Careful though the App Data folder I think is exclusive to windows. And yea I have that window plugin on my website if you'd like to use it.
 

Kyuukon

主人公
Veteran
Joined
Aug 22, 2013
Messages
2,217
Reaction score
1,076
First Language
Spanish
Primarily Uses
RMMV
I see. I did not think it through. I want my game to be available for all platforms if possible!

Thanks for the heads up (and the plugin!) :p
 
Joined
Dec 31, 2015
Messages
82
Reaction score
26
First Language
English
Primarily Uses
RMMV
I'm kind lf not understanding how to use this.
I looked at previous your tutorials, but I'm unable to u derstand what Node.js is.
Is the code you wrote in this tutorial enought, or do I need to download another file for it?
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
NodeJS is a tool for running JavaScript on your computer. This allows you to do things like download files, create directories, create servers for online games and more.

MV comes with NodeJS as a part of the framework MV games for desktop are built on.

The code in the tutorial is enough. It will not work on browser based games however.
 
Joined
Dec 31, 2015
Messages
82
Reaction score
26
First Language
English
Primarily Uses
RMMV
NodeJS is a tool for running JavaScript on your computer. This allows you to do things like download files, create directories, create servers for online games and more.

MV comes with NodeJS as a part of the framework MV games for desktop are built on.

The code in the tutorial is enough. It will not work on browser based games however.
All right, thanks.
So I think I get how to write files.
But I'm a bit confused how to read files and check their value.

I think I use the "MVNodeFS.readFile = function (filePath, filename)" as a script. Do I insert that in a Conditional Branch script if I want to check the content of the file?

I'm sorry, I'm not good at JS, but I kind of need this feature in my game.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
Explain the exact feature you need, that would make it easier to recommend something.
 
Joined
Dec 31, 2015
Messages
82
Reaction score
26
First Language
English
Primarily Uses
RMMV
What I would need is to check if a certain file exists in a certain directory, and if it does, read its value and compare it to a specific text string defined with a plugin command, and if it matches, return true.

Something sort of like "if data/newgameplus.txt = 'unlocked'"
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
515
Reaction score
676
First Language
English
Primarily Uses
RMMV
Okay, to do that you'd need to look at one of my other tuts. It tells you how to create directories and I believe read directories.

Reading a text file can be done with the functions mentioned above. Once you do that the data will be in string format, so you'll have to figure out what you want to check exists in that text file.


For your purposes if you want to do values a JSON file might be better. If you want info on that I can help you.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

a_a
As if my week could not be more shitty, my Iphone is currently stuck on the apple screen. I'm decently tech savvy so I'm going through all the options b4 I have to go to apple, but from the signs I'm pretty hosed.
Rip a buncha music I had on there, photos from trips, and every message. I shoulda backed it up :(

R.I.P Fbi Surveillance Van #5296 (6s)
Its Chinese new year today! Happy year of the rat~
just got back from the doctor about my back - 3 -)~ will know in a week or so's time how bad the problem is. Overall doing better though! not hurting so badly.
Oh God.. I'm so in love using "this" keyword in my codes. It is so more readable than my previous global objects xD
How is it possible that Paper Mario 2 is both longer than I thought but shorter than I remember?

Forum statistics

Threads
93,590
Messages
913,763
Members
123,139
Latest member
younes
Top