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!
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!