Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
562
Reaction score
805
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

MVNode1.png


In-game Example

MVNode2.png


Console Log

MVNode3.png


File Within Directory

MVNode5.png


File Output

MVNode4.png


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,965
Reaction score
3,072
First Language
French
Primarily Uses
RMMV
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
562
Reaction score
805
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,257
Reaction score
1,117
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...
MVNode2.png

I wantz that BGM playing window plugin xD!!
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
562
Reaction score
805
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,257
Reaction score
1,117
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
27
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
562
Reaction score
805
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
27
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
562
Reaction score
805
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
27
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
562
Reaction score
805
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.
 

Jatopian

Veteran
Veteran
Joined
Jul 5, 2015
Messages
55
Reaction score
16
First Language
English
Primarily Uses
Hello Kino. You left a reply to a comment on your old site stating that you had a platform-independent approach to file management, or at least one that works on mobile/browser. However, I can't find where you ever posted it. Do you happen to know?

I am trying to detect and handle the case of an image resource file (like a faceset) not being present as expected, so that an MV game might do something besides crash when this happens, such as use a fallback graphic. It would be nice to have or make a platform-independent plugin to call, or at least a plugin for each platform. The only one I know is by ozubon and it is based on your node.js platform-dependent tutorial here.
 
Last edited:

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
551
Reaction score
238
First Language
English
Primarily Uses
RMMV
For browser/mobile compatibility you need to use XHR, which is short for XMLHttpRequest. You can look up how it works on MDN, or check out how the engine does it in rpg_managers.js. Detecting whether a file exists is possible - if it doesn't exist, the error function will be called.
 

Jatopian

Veteran
Veteran
Joined
Jul 5, 2015
Messages
55
Reaction score
16
First Language
English
Primarily Uses
For browser/mobile compatibility you need to use XHR, which is short for XMLHttpRequest. You can look up how it works on MDN, or check out how the engine does it in rpg_managers.js. Detecting whether a file exists is possible - if it doesn't exist, the error function will be called.
As best I can tell, the engine doesn't use XHR, except in the case of DataManager.loadDataFile, which uses it asynchronously. Unfortunately, every resource I find online about checking whether a file exists using XHR specifies that one should use a synchronous request, but that approach just gets the browser console to spit an error about how synchronous XHR requests are deprecated while the game complains about a "network error".
 

Latest Threads

Latest Posts

Latest Profile Posts

A side story is almost getting into the next stage. Almost everything in this game has real-life references.
unfinishedbuildingwip1.jpg
Everyday life is full of absurdity. This is just a reflection.
TFW you discover how the script batching works in VNMaker... after you've had the program for almost 3 years.
Writes that the characters have to climb a mountain.

Realized that I have to draw six sets of models where characters are climbing.

At least I know how to draw something. :)
I'm currently scratching my head on how to design plains. What do I even put in plains? Some trees (but not too many or it turns into a forest)? Grass? A few bushes? That's a bit dull.
I'm looking for tutorials but Google didn't seem to find much (or maybe I have the wrong keywords). I don't know how to map plains and I don't know how to learn.

Forum statistics

Threads
111,406
Messages
1,060,894
Members
144,759
Latest member
Belphegor
Top