RPG Maker MZ Plugin Tutorial
RPG Maker MZ Plugin Tutorial
1.0 Introduction
1.1 Who is this tutorial for?
2.0 Guide to Using Plugins
2.1 What is a Plugin?
2.2 Installing a Plugin
3.0 Super Quick JavaScript Tutorial
3.1 Introduction
3.2 Start off by running a script!
3.3 Variables
3.4 Control Structures: Conditional Branches
3.5 Control Structures: Loops
3.6 Functions
3.7 Using Core Script Functions
3.8 MDN Web Docs
4.0 Making a Plugin
4.1 Preparations Conducted Before Making a Plugin
4.2 Deciphering an Official Plugin
4.3 Explanation about Annotations
4.4 Points to Consider When Making an Asset Public
1.0 Introduction
It is intended for users with a grasp and understanding of both the general flow of game
production and the basic functions of either MZ or a previous PC version of RPG Maker. This
tutorial will only provide a minimal level of explanation concerning JavaScript knowledge and
syntax.
2.0 Guide to Using Plugins
What to prepare:
● An IDE (for example, Visual Studio Code, Atom, Notepad++, etc.)
● Web browser (Google Chrome or other browser used for viewing the internet)
Downloading a Plugin
Plugins range from files provided officially to those provided and sold by individuals. First,
begin by acquiring the JS file in question.
When obtaining a JS file over the internet, download it according to the standard methods
specific to your browser. Do not change the filename unless provided with specific
instructions to do so.
The specific distribution method will depend on the creator. These various methods include
placement on a service such as Dropbox, compression, or distribution through GitHub or as
an individual JS file or sample project.
Try opening the file you downloaded using a text editor. If the text appears in a garbled
state, the file may not have downloaded properly.
This will bring up a list of the plugins you have already installed for your project. As a new
feature of MZ, we have introduced a checkbox on the left of the list. Now you can simply
switch plugins ON/OFF. You can change the state of multiple plugins by shift-clicking a range
of them and pressing SPACE.
Warning messages will also now appear on the bottom of the screen. These are messages
that are displayed when there is a strong possibility that a plugin will not function properly if
you begin the game in that state.
Types of Warnings
Warning messages are of the following types, each of which requires the appropriate
response:
3.1 Introduction
Scripts in MZ are a means of accessing the fundamental parts of a game's operations and
specifications. As all game-related areas in MZ are implemented using the language known
as JavaScript, scripts are also specified according to the syntax used by JavaScript.
By mastering scripting, you can carry out game development at a much more advanced
and efficient level than just constructing events through event commands.
So that you may reach this goal through minimal effort, we have omitted overly detailed
explanations and refrained from rigorous descriptions in some areas. Please be sure to keep
that in mind.
About JavaScript
JavaScript is a programming language that was originally intended for web browsers. Due
to the introduction of Node.js, it has been applied to a wide range of uses in recent years,
such as server-side processing.
Please be aware that, if you look at the JavaScript source on general websites with the aim
of mastering scripts using MZ, you may be taking a fairly circuitous route.
As it shares a similar name, be careful not to make a mistake when purchasing a book or
searching the internet.
After the title screen appears, press F8. This should open a window that is separate from
the game, which provides developer tools that will be especially helpful when developing or
debugging a game using plugins.
Log Output
Developer tools provide a large number of functions. For now, start by selecting the
Console tab. It should appear as shown below.
You can enter text on the white screen. First, try inputting the following statement.
This should produce the result shown above. Just like that, you have run JavaScript and
verified the results!
The script you executed is a command that tells the console to display the string Hello
RPG Maker MZ.
As its name suggests, console.log is a command that logs a message to the output
console, which allows you to check values and states at runtime.
Perhaps there are users who will think, instead of using this type of tool, that they would
rather run various scripts using the Script event command. However, we cannot recommend
that method due to the following reasons:
So, first, learn basic JavaScript syntax while outputting logs using developer tools, before
aiming to use scripts in event commands.
Comments
The event command Comment makes it easy to write remarks about complex processing
using an event command. There is also a function that corresponds to Comment in
JavaScript.
This function is itself called a "comment." If you write //, the subsequent string will become
a comment; it will then be ignored when you run the program.
If you wish to define a comment spanning multiple lines, you can use a block comment as
shown below.
/*
* If you wish to define a comment spanning multiple lines,
* you can use a block comment.
*/
console.log('This text will be outputted.');
By making skillful use of comments, you can create a program that is easy to understand.
3.3 Variables
In MZ, there is a concept called a variable. From the event command Control Variables, it
is possible to specify a name and freely assign or change a value. The same type of system
is also found in JavaScript.
In this chapter, this tutorial will explain the differences between variables in JavaScript and
MZ and their varying methods of use.
Declaring a Variable
First, as opposed to variables in MZ, you must declare a variable when using JavaScript.
When declaring a variable, you convey to the program that you are defining a variable
under a specified name. You cannot use a variable if it is not declared.
let aaa = 0;
In the above example, we are declaring that we will use a variable with the name of aaa
and, at the same time, we are assigning it a value of 0. Although a value of 0 is assigned
automatically to an event command variable, unless an initial value is assigned to a
JavaScript variable, it will have no value at all. More precisely, the special value undefined is
assigned signifying that the variable has no defined value.
let is a syntax-defined word that is required for making a declaration. In addition to let,
there are a number of words and terms that are syntactically defined by JavaScript. These
are collectively known as reserved words.
For the variable name aaa, you can enter a name of your choosing. However, you cannot
use certain text including reserved words, which this tutorial just introduced, and some
symbols.
Use let unless you have a particular reason or point of focus that requires otherwise.
There is also another reserved word for declarations, const. Perhaps you are concerned
that, just for declaring variables, there are too many different types. It would be difficult to
explain about const at this point, so we will save that for later. For now, just be sure to learn
about let.
Calculating Values
From the event command Control Variables, in addition to assigning values, it is possible to
conduct basic calculations such as addition and multiplication. Naturally, you can do the
same for variables within JavaScript.
First, define a variable.
let x = 1;
Next, write the respective calculation in the manner shown below.
Addition x = x + 1; 2 x += 1;
Subtraction x = x - 1; 0 x -= 1;
Multiplication x = x * 2; 2 x *= 2;
Division x = x / 2; 0.5 x /= 2;
Modulo x = x % 2; 1 x %= 2;
Then the variable x will store the result of the calculation. Enter console.log(x); in the
console to check the value it contains. To execute the calculation, you can use either the
script or the shortened script. So you can see that it is possible to control variables in the
same way as when using the event command.
Additionally, when adding or subtracting a value of 1, you can shorten the statement in the
following ways:
x++;
x--;
These are called the increment operator and decrement operator, statements frequently
used in programming.
Types of Variables
When entering variables using the event command, you can only use a numerical value,
unless using the Script operand which allows you to use strings. With JavaScript, you can
also store other values in variables besides numbers. The following is a list of the most
typical kinds of values:
A boolean value functions in the same manner as a switch when using an event command.
When a variable is switched ON or OFF, that is expressed as either true or false,
respectively.
null expresses an empty state. It represents a different concept than the aforementioned
undefined. It could be said that undefined expresses a state in which no value has been
defined, while null expresses a state in which a value of nothing has been defined.
Array, object, and function variables are fairly complex, so they will be explained in later
sections.
Arrays
Among the types of variables explained in the previous section, arrays and objects are
somewhat complex. Using these variables, you can work with multiple values in an
organized way.
An array is a variable that manages a group of values as a single entity.
console.log(aaa[0]);
The value 0 specified within the square brackets is called an index. It indicates which part
of the array will be referenced. The values in the array itself, such as 1 and 2, are called
elements.
The index for the leftmost element is the number 0, and increases in order to 1, to 2 and so
on. In other words, when running the above script, the element at index 0 (1) will appear.
Arrays also possess a property called length. By referencing this property, you can acquire
the number of elements in an array.
console.log(aaa.length);
Objects
So, when using an array, you could use numerical values as indexes and unify multiple
values in one entity. To use strings as indexes, use an object instead. An object is similar to
an associative array or structure in other languages. (Strictly speaking, to provide a slightly
different explanation, there are formal associative arrays used separately in JavaScript.
However, that has not been included in this section.)
With an array, you can reference values using an index. For an object, strings such as bbb
and ccc are generally called properties.
In a similar manner to an array, to reference an object’s contents, write a statement such as
the following. When inputting a string, remember to enter single or double quotes at the
beginning and end:
console.log(aaa['bbb']);
Running the above statement this will return 1. When using an object, you can also
reference properties in the following way:
console.log(aaa.bbb);
With this method, you do not need to include brackets or quotes, though you cannot use
symbols in the property name (with a few exceptions such as underscore).
Referencing variables through properties using a period like this is a method frequently
employed by the core scripts. Make sure you have a strong grasp of these basic points.
In terms of the meaning of aaa.bbb, it references the property bbb from the object aaa.
Practical Application
In this section, a tutorial will show you how to run complex programs using the knowledge
learned up to this point. Please make sure that you have gained a proper understanding of
the material before you begin.
Try to run the following script, but instead of just copying it, be sure to enter it directly
yourself.
let obj = {prop1:0, prop2:1};
obj.prop1 += obj.prop2;
let propName = 'prop3';
obj[propName] = 5;
let num = obj.prop3;
console.log(obj.prop1 + obj.prop2 / num);
The first point deserving special notice is step #4. This is where you assign a variable to
the property. In JavaScript, you can reference a property name using the value of a variable.
Next, we will discuss step #6. When using the event command, only a single calculation
could be executed per command. On the other hand, with JavaScript, you can conduct
multiple calculations in a single operation in the same way as actual mathematics. When it
comes to mathematics, you probably know that mathematical calculations are assigned an
order of operations. For example, when conducting addition and multiplication, multiplication
is calculated first.
When you manually enter the above script, upon entering obj in the second line, a list like
the one shown below should pop up.
● Structure
if (conditional expression) {
This procedure will execute if the condition is met.
}
● Example
let aaa = 1;
if (aaa <= 1) {
console.log('ok1');
console.log('ok2');
}
The area encompassed by curly braces is known as a block. A block is a set of statements
that will be run in the same scope. In addition to if statements, blocks will be used from here
onwards, so be sure to learn this concept fully.
Within the event command Conditional Branch, you will find the checkbox Create Else
Branch. This can be reproduced within an if statement.
let aaa = 1;
if (aaa <= 1) {
console.log('ok1');
console.log('ok2');
} else {
console.log('ng1');
}
Following the block for the if statement, you can enter else and create another block. The
latter block will be executed if the condition is not met.
With JavaScript, you can combine these branches in one statement. A and B is expressed
as A && B, and A or B is expressed as A || B.
let a = true;
let b = false;
if (a && b) {
console.log('The conditions are not satisfied because, although a is true, b is
false.');
}
if (a || b) {
console.log('The conditions are satisfied because, although b is false, a is true.');
}
Another type of logical operator, !, expresses negation (often referred to as “not”). This
returns a result that inverts a boolean value.
let a = true;
if (!a) {
console.log('The condition is not satisfied because a is true and the result inverts
that value.');
}
string empty string ('') variables other than that shown on the left
(including the string '0')
● Structure
conditional expression ? expression if the condition is satisfied : expression if the
condition is not satisfied;
● Example
let aaa = 1;
let bbb = (aaa === 1 ? 2 : 3);
console.log(bbb);
In the above example, the value assigned to aaa is 1, so 2 will be assigned to bbb.
You can also substitute conditional operators for the if statements used above. The benefit
of a conditional operator is that it allows you to express a conditional expression within one
overall expression.
With MZ, you can also use scripts for mathematical formulas for skills and items. However,
the entry field has only one row.
In the following example, to the damage formula for a normal attack, a judgment based on
a conditional operator has been added. Although it is probably an unfamiliar notation, the
conditional expression b.isStateAffected(5) signifies the condition that the attack target is in
the state represented by State ID[5].
As you may have noticed already, the event commands used within RPG Maker were
originally created while referring to the variables, conditional branches, and other control
structures that are found within programming languages.
Loops Using while
The most basic loop procedure is a while statement. Using the event command, if you
create a conditional branch within a loop in order to stop it, you will be unable to specify a
closing condition for the loop. You can, however, designate a closing condition by using a
while statement in JavaScript.
● Structure
while (conditional expression) {
This procedure will run repeatedly.
}
● Example
let a = 0;
while(a < 10) {
a++;
console.log(a);
}
In the above example, as long as a < 10 is satisfied, the operations contained in the block
will be executed repeatedly. This will provide a log output of numbers spanning from 1 to 10.
In such a case, not only would it stop the game, it would also place a burden on your PC.
Be sure to carefully discern whether or not there is a mistake present in the language
specified for the conditional expression.
Loop Breaks
The procedure conducted by the event command Break Loop is also provided in
JavaScript. It is known as a break.
let a = 0;
while(a < 10) {
a++;
console.log(a);
if (a >= 5) {
break;
}
}
In this example, the loop was discontinued at the point it finished outputting numbers up to 5.
● Structure
for (initialization; conditional expression; increment expression) {
This procedure will run repeatedly.
}
● Example
for (let i = 0; i < 5; i++) {
console.log(i);
}
In the above example, this will provide a log output of numbers spanning from 0 to 4. i is
typically known as a counter variable. As this is a standard type of variable, you can select
any name you like. However, i is often used here.
You can also use for statements when we want to execute the procedures for an array
repeatedly.
● Structure
for (declare variable for index in array) {
This procedure will repeat.
}
Using for-in, the array's indexes, meaning numbers spanning from 0 to 4 in order, will be
entered into the variable i. As a result, in the same manner as the example in the previous
section, this will output all elements in the array, meaning 1, 3, 5, 7, 9.
● Structure
for (declare variable for property name in object) {
This procedure will repeat.
}
● Example
let aaa = {bbb:1, ccc:2};
for (let property in aaa ) {
console.log(aaa[property]);
}
In the above example, the property names bbb and ccc are entered into the variable
property. As a result, 1 and 2 will be outputted in the log. It is determined randomly whether
1 or 2 will be outputted first. Be sure to be aware of that point.
● Structure
for (declare variable for element of array) {
This procedure will repeat.
}
● Example
let aaa = [1, 3, 5, 7, 9];
for (let data of aaa) {
console.log(data);
}
As for-of is the more convenient of the two, it is used frequently within core scripts.
However, as opposed to for-in, for-of cannot be used for an object.
3.6 Functions
As you read and decipher core scripts and master the use of scripts, functions are the most
important concept you will come across.
In a programming language, a function is something that allows you to easily call for
multiple procedures in a united fashion. If you define within a function the procedures you
want to run many times, by merely calling that function, you can then execute those
procedures.
For the time being, you can consider this as a similar concept to a Common Event.
Defining a Function
In order to execute a function, first, you must define the function that will be called.
● Structure
function functionName() {
procedure you wish to execute
}
● Example
function aaa() {
console.log(111);
}
In the same way as with variable names, you can freely assign the function name.
Likewise, you cannot use reserved words or symbols excluding a few exceptions.
The fact is, if you make good use of scripts, you will not have many opportunities to define
functions. That said, core scripts are actually an enormous collection of functions. At this
point, the key thing to know about functions is that they are especially important to learning
about scripts.
Calling a Function
The expression used to call a defined function is shown below.
● Structure
function name();
● Example
aaa();
In this way, we add parentheses after the function name and then call it. The majority of
procedures written in the event command Script call a function. For this reason, be sure to
properly remember this notation method.
Arguments
When calling a function, you can pass parameters such as numbers and strings to the
caller of that function. This method is known as an argument. This concept is not found in
Common Event, so it may be a little confusing. For now, this tutorial will recall a function that
is used in mathematics.
y = f(x)
Functions used in mathematics follow the line of thinking that x is for input and y is for
output. An argument is equivalent to the value x expressed in the above example.
● Structure
function function name (parameter(s)) {
procedure you wish to execute
}
function name (argument(s));
● Example
function outputSum(bbb, ccc) {
console.log(bbb + ccc);
}
outputSum(10, 11);
If you execute the example procedure, a log of 21 will be outputted. The steps involved in
this procedure are as follows.
1. By calling the function outputSum, you pass the argument 10 and the argument 11.
2. Processing shifts to the function outputSum, and the arguments 10 and 11 are stored
in the parameter bbb and the parameter ccc, respectively.
3. The value of ccc added to bbb in the console.log, meaning 21, will be outputted in the
log.
Return Value
The return value is the result given when executing a function. Once again, recall that
mathematical function.
y = f(x)
The above value y is the return value. Please look at this example.
● Structure
function function name() {
return value you wish to return
}
let aaa = function name();
● Example
function getHundred() {
return 100;
}
let bbb = getHundred();
console.log(bbb)
When executing the example function getHundred, the fixed value 100 will be returned as
the return value, meaning 100 will be stored in the variable bbb.
● Structure
let function name = function() {
procedure you wish to execute
}
function name();
● Example
let aaa = function() {
console.log(1);
}
aaa();
As you are able to define a function within a variable, you are also able to define it within
an object's property. This is an especially important point. A function that is defined as an
object's property is specially referred to as being a "method." However, in this section, this
tutorial will consistently use the term "function" throughout.
So, if you are able to store functions within variables, what benefits might that bring? In
truth, when using the event command Script, you are given almost no opportunity to enter
functions within variables. Nevertheless, if you correctly understand this concept, when
calling functions defined in core scripts, you will be able to accurately understand why they
are written in such a way.
By using the => symbol, you can define the function with a smaller number of characters. If
there is only one parameter, you can also omit the parentheses.
Also, if you are able to express the contents of a function within one expression, you can
omit the block (braces) or the use of return to return a return value.
Functions defined through arrow functions handle the execution agent this differently than
normal functions. At this stage, it would be difficult to explain those differences, so this
tutorial will not include that information.
Acquiring Variables
The script used to acquire the contents of game variables is shown below. Thanks to the
developer tool, when you input this text up to $game, $gameVariables will appear as a
suggestion.
$gameVariables.value(1);
First, take a look at $gameVariables. As an object that handles game variables, it has been
defined as a core script. Next, there is value. This is a function that is a property owned by
$gameVariables. It has also been defined within the core scripts.
The argument 1 is the variable number. In other words, when running the above script, this
will return the value of variable number 1 as the return value.
If there is any part of this explanation that you are unable to understand, be sure to read
the section about functions one more time.
Tip: Global Variables
Variables that can be referred to anywhere within a program are known as global variables.
A number of these have been defined within the core scripts. Including $gameVariables;
almost all of the variables that begin with the symbol $ are global variables.
Within JavaScript, variables declared on the top level (area not within some kind of function
block) are treated as global variables. (Variables that are used without being declared are
also considered global variables. However, if you try to use global variables by utilizing this
method, this may cause an error in an unrelated area. It is therefore an exceptionally risky
method.)
Setting Variables
Next, the tutorial will explain scripts for setting the values within game variables.
$gameVariables.setValue(1, 3);
Same as value, setValue is also a function possessed by $gameVariables. This has two
parameters. The first parameter contains a variable number and the second holds a set
value.
After you execute a script for setting a value, if you run a script for reacquiring the value,
you should be able to confirm that the set value has been reflected.
Database Acquisition
MZ possesses enormous databases. All of the values entered here are stored in the global
variable $dataXXX.
$dataActors[1].name;
When you run the script, it should return the name of the first actor recorded in the
database Actors. $dataActors is an array variable defined by a core script. It is an object with
indexes that store a database ID and elements that store the value recorded for actors in the
database. The element at index 0 is null, so be careful in that regard.
name is one of the properties owned by the object corresponding to each respective
$dataActors element. It stores the value entered for a name.
Other databases are also defined based on the same essential ideas. A list of those
databases appears below:
$dataActors actors
$dataClasses classes
$dataSkills skills
$dataItems items
$dataWeapons weapons
$dataArmors armor
$dataStates states
$dataAnimations animations
$dataTilesets tilesets
As it would not be possible to cite all of the properties of each database at this time, you
will find a separate reference here. By consulting the reference, you can obtain all of the
information included in the databases.
https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mz-script-call-reference.122
501/
Within the following reference, you will find a compilation of the specific methods for
obtaining data by means of each object.
https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mz-script-call-reference.122
501/
The game objects included in the above table can be broadly divided into two categories:
objects known as $gameXxx and objects named XxxManager.
$gameXxx retains functions and data in a combined fashion. For example, $gameParty
holds data pertaining to the current party, and at the same time, provides a function that
adds members to the party.
https://developer.mozilla.org/ja/docs/Web/JavaScript
As these docs are not specifically intended for MZ, it goes without saying that all of this
information will not be applicable. However, if you have come to a dead end while you are
learning JavaScript, you can surely find the hint you need to solve your problem if you
search therein.
In terms of the other functions available, that information is compiled within the
aforementioned MDN Web Docs.
Arrays
Here, you will find explanations of functions that are frequently employed when dealing with
arrays. To call an initialized variable, you can use [].
Many arrays can be found within the data defined by core scripts. If you gain a mastery of
the types of array functions and their methods of use, this will prove exceptionally helpful in
writing efficient scripts.
Function Definition
pop Extracts and returns an element from the end of the array
These functions can be used in place of the for statements explained earlier. If you utilize
them properly in line with their intended use, you will be able to write easy-to-read scripts
that have very few bugs. These functions are also used frequently within core scripts, so be
sure to remember them.
Additionally, these functions work well in tandem with arrow functions. Be sure to master
them together with the method for writing arrow functions.
Function Definition
forEach Executes the function passed to an argument for all elements within an array
map Returns a new array containing elements comprised of the return values of a
function passed to an argument
filter Returns an array that only includes elements with a return value of true from
a function passed to an argument
find Returns the first element with a return value of true from a function passed to
an argument
findIndex Returns the first index with the return value of true from a function passed to
an argument
some Returns true if true is returned from even one of the return values from a
function passed to an argument
every Returns true if true is returned from all of the return values from a function
passed to an argument
reduce Consolidates the array into a single value, using a function passed to an
argument
reduce is fairly complicated, so this tutorial will not include a full explanation in this section.
An in-depth explanation is provided in MDN Web Docs.
● Other Functions
JavaScript provides a large number of other functions that are useful when dealing with
arrays.
Function Definition
includes Returns true if the value specified in an argument is contained within an array
join Returns a string consolidating all elements in an array using the separator
string specified in an argument; commas will be used if the separator string is
omitted
splice Allows you to delete or insert array elements by specifying an index; specify
an index in the first argument, the number of values to delete in the second
argument, and the elements to insert in the third argument and beyond
Math
Math modules are a set of functions in which the functions and constants used in
mathematics are defined. As opposed to the arrays described in the previous section, they
are not used in relation to variables they have defined. They call a Math function that has
been defined in a fixed manner, such as Math.abs.
From among these Math modules, this section will explain about the functions that are
used by core scripts and for mathematical formulas dealing with areas such as skills.
Function Definition
Math.ceil(n) Returns the smallest integer exceeding the number n; can be used
for operations such as rounding up a decimal point, but not for
designating a negative value for an argument
Math.floor(n) Returns the greatest integer not exceeding the number n; similarly,
can be used for operations such as rounding down a number, but
not for designating a negative value for an argument
Math.trunc(n) Returns the value of number n with the decimal point discarded;
can be used to round down a number more expressly in
comparison to floor; floor is used more often by core scripts
Math.randomInt(n) Returns a random integer within a range from 0 to n-1; this function
was added, in fact, by core scripts and is not written about in MDN
Strings
Here, this section will explain about functions that are frequently employed when dealing
with strings. Just like arrays, strings are often used by core scripts. If you gain a grasp of the
most convenient functions, this will prove helpful when writing scripts.
toLowerCase Returns a new string that is a lowercase letter version of the string
substring Returns a string that is a portion of the original string spanning from
a starting position to an ending position specified in arguments
replace Replaces the string specified in the first argument with the string
specified in the second argument
split Returns strings after splitting them into an array, using the
character (or characters) specified in an argument as a separator
string
Editor Installation
Plugins are principally written in JavaScript. As a plugin file contains a text format, it is
possible to develop it through a commonly used notepad application or text editor. However,
it is recommended that you prepare a dedicated editor specially designed for JavaScript
development.
If you search for a dedicated editor, you will find many articles that provide comparisons.
Choose whichever editor meets your preferences. It is a good idea to refer to newer articles
in terms of when they were posted.
● Code completion (after you write code partially, predicts and suggests the
subsequent notation)
● Static analysis (provides an advance warning regarding notation that may cause a
compilation or runtime error)
● Formatter (conducts automatic formatting for indents and other aspects of coding
according to rules set beforehand)
● Debugger (stops script execution during runtime, and checks variables or runs code
line by line)
Plugin creation is a complex and delicate type of work. By making use of these editor
functions, you can proceed in an easy and efficient manner.
File Creation
As you probably know if you have already used plugins, plugin files are placed in the
project folder [js/plugins/]. Now, create a new JS file.
4.2 Deciphering an Official Plugin
There are, of course, various kinds of notation found within plugins. Using the official plugin
TextPicture.js as an example, this tutorial will now explain about standard plugin notation
methods.
(() => {
// All procedures are written here.
})();
Although it may look complicated with so many brackets being used, what this is simply
doing is defining functions and executing them therein. There are two reasons why you
expressly create functions and write procedures to allow for immediate execution.
● This ensures that the variables defined in the plugin will not affect other plugins.
● This allows for use strict to be utilized.
As explained in the previous chapter, when you declare a variable outside of a function, it
becomes a global variable. As global variables can be referenced from all areas including
other plugins, it may cause conflict if you define the same global variable using multiple
plugins.
As a basic rule, unless a global variable is truly necessary, you should not define it as such
without good reason. When you define a variable within a function, you can limit its scope
(valid range) to within that plugin only.
Although use strict has not been included in the official plugin used as an example, it is
recommended that you use it because, when you write it, it will tell you beforehand about
any notation likely to cause a bug. While this method of use may seem somewhat unusual,
you will define only the string use strict at the beginning of the function, as shown below.
(() => {
'use strict';
// do something
})();
For example, if you try to use a variable without declaring it, it would then be treated as a
global variable. If enabled, use strict would stop that from proceeding because it has been
judged as an error.
Writing Help Comments
Next, please look at the plugin example's comment area. In an MZ plugin, an area
beginning with /*: is used for special comments. Within the comments, you will also see a
number of notations beginning with @. These are read and analyzed as help information
within MZ's editor, and are called annotations.
/*:
* @target MZ
* ….
*/
There are many kinds of annotations. This tutorial will explain more about them in the next
chapter.
First, in the initial line, this declared the variable _Game_Picture_show and assigned the
core script function Game_Picture.prototype.show. Next, in the second line, this gave a new
definition to the function Game_Picture.prototype.show.
Finally, in the third line, this executed the function assigned to the variable
_Game_Picture_show. apply is a function (method) used to execute a function assigned to a
variable.
In terms of what will be carried out through this chain of procedures, after calling the
original procedure of the core script function Game_Picture.prototype.show, it has newly
introduced the procedure that the plugin creator wants to add. While repeatedly redefining
functions in this way, you can create plugins.
This specifies the plugin name in the first argument, the command name in the second
argument, and the function that will be called when executing the command in the third
argument. Within the argument args, the function for the third argument, the parameters
specified when calling the command are stored in an object format. In the previous version,
MV, they were passed using an array. So, that too has changed.
In the example plugin, this concept is used in the following manner.
Tip: Closures
During command processing for the plugin example, this section rewrote the value of the
variable textPictureText that was defined outside of the function. Although it might seem
strange at first glance, this has come into effect by means of a structure known as a closure.
While the scope of textPictureText falls within the range of the block defined, that also
includes what is contained within a nested function. That is because, with this concept, the
scope (lexical scope) is determined at the point a function is defined.
Regardless of the structure itself, this concept is very useful when creating plugins.
Core scripts use const as a general rule, and employ let only when looking to reassign a
value. By using these words appropriately in this manner, it becomes easier for a code
creator to convey their intentions.
Object Creation
The following type of notation appears within the function createTextPictureBitmap.
new is a word you will use if you want to create an object. This tutorial previously explained
that you write the following type of notation when creating an object.
If created using {}, an empty object will be generated. On the other hand, when creating an
object using new, to put it simply, you will generate an object in a state in which its properties
and functions are defined according to an already determined blueprint.
In the above example, as it has specified Window_Base as the blueprint, it will create
tempWindow in a state in which its functioning as a window has been prepared from the
start.
Although this tutorial has explained that core scripts are a collection of functions, it might
actually be more appropriate to say that they are a collection of these kinds of blueprints.
new is also used frequently within core scripts, so be sure to learn how to use it at this time.
First, look at plugin command implementation. Here, after executing the command set, it
has saved the string that it wants to be drawn as a picture to the variable textPictureText.
Next, add that to the picture display procedure. Game_Picture.prototype.show is used for
the method displaying the picture. Even if you did not know about its specific
implementation, you could probably understand what it does just from its name.
Through the procedures added now, if the picture name is blank (image specified as n/a)
and the plugin command has been executed, the string to be drawn will be retained and a
flag will be set.
This would then point to the result that the picture has not been drawn at the present time.
This can happen because, within the core script, the class Game_Picture holds the state of
the picture, while the class for the actual picture image Sprite_Picture exists separately.
When using the class for the actual picture image, this monitors the state of Game_Picture
for each frame. Not limited to this class, there is a method with the name of update that
provides many possibilities in that it checks and updates the state of the picture as each
frame is executed.
The actual judgment procedure is a little long, so please check the code. To simply explain
its implementation, it monitors the contents of Game_Picture and, if judged to be necessary,
executes the creation or destruction of the picture.
createTextPictureBitmap
destroyTextPictureBitmap
A sprite holds information such as an image's display position, magnification, and color
tone, while a bitmap simply holds color information for each X/Y coordinate.
Annotation Definition
@plugindesc The plugin's title; text written here will be displayed on a list on the
Manager Screen
@help Text explaining detailed methods for using a plugin; displayed on the
Manager Screen
Plugin Parameters
Plugin parameters are functions that allow plugin users to set values of their choosing. The
following is an example of their settings.
* @param paramName
* @text parameter name
* @desc parameter description
* @default
* @type number
Annotation Definition
@param Name of the parameter; used as a property name when acquiring the
contents of a parameter through the plugin's implementation area;
written at the very top of the annotation defining parameter contents
@text Display name of the parameter; displayed on the screen where the
parameter is inputted
@default Value set automatically when the plugin is turned ON (Please note
that the value is not set automatically when the parameter is empty.)
@type Data determining the parameter's type; changes the input dialog's UI
based on the value specified here; when specifying number, for
example, provides a parameter for which only numbers can be
entered
@parent Allows you to specify a parameter acting in a parent role; through the
assignment of parent-child relationships, enables the construction of
parameter trees
Out of these annotations, @type probably requires the most detailed explanation. Based
on the type specified using @type, the dialog's contents will change. A plugin will become
significantly easier to use if you specify the appropriate types.
string Brings up a standard string input field without any inputted value
particular restrictions
multiline_ Brings up a string input field in which multiple lines can be inputted value
string entered
file Brings up a dialog for selecting a file for an image or selected file name
sound, etc.; the file selected here will not be subject to (with no
the function deleting unused assets extension)
number Brings up a field in which only numbers can be entered inputted value
actor Brings up a dialog for selecting an actor from the ID of the choice
corresponding database selected
class Brings up a dialog for selecting a class from the ID of the choice
corresponding database selected
skill Brings up a dialog for selecting a skill from the ID of the choice
corresponding database selected
item Brings up a dialog for selecting an item from the ID of the choice
corresponding database selected
weapon Brings up a dialog for selecting a weapon from the ID of the choice
corresponding database selected
armor Brings up a dialog for selecting an armor piece from the ID of the choice
corresponding database selected
enemy Brings up a dialog for selecting an enemy character from ID of the choice
the corresponding database selected
troop Brings up a dialog for selecting an enemy group from the ID of the choice
corresponding database selected
state Brings up a dialog for selecting a state from the ID of the choice
corresponding database selected
animatio Brings up a dialog for selecting an animation from the ID of the choice
n corresponding database selected
tileset Brings up a dialog for selecting a tileset from the ID of the choice
corresponding database selected
common Brings up a dialog for selecting a common event from the ID of the choice
_event corresponding database selected
struct<ty Brings up a dialog allowing for entry within multiple fields {aaa: "input value",
pe at one time bbb: "input value"}
name>
struct is a parameter that defines multiple parameters in a combined fashion. For the type
name, specify any string that does not contain a symbol. For the type definition, place that
information within a separate section as shown in the explanation below.
/*~struct~type name:
*
* @param param
* @text parameter name
* @desc parameter description
* @default
* ......
*/
It is also possible to then place struct in an array. Some of the official plugins will serve as a
good reference regarding the specific implementation methods.
@value select Value actually set in the parameter when the pull-down
combo option is selected; in short, the value of @option is set in
the parameter
Plugin Commands
With the specifications for plugin commands being retooled in MZ, it is now possible to
represent command and argument definitions using annotations. If the command name and
argument data are defined beforehand, the user can call the command just by selecting it
from the event command.
* @command COMMAND
* @text command name
* @desc command description
*
* @arg arg1
* @text argument name
* @desc argument description
Annotation Definition
For the plugin command and its argument, you can designate annotations in the same way
as parameters and also specify display names, detailed explanations, default values, and
type names.
Annotation Definition
@base Specifies the name of the base plugin; displays a warning if, without
entering the base plugin, you only enter the plugin in question
@orderAfter If applied, specifies the name of a plugin that must be ordered higher;
used for reasons such as preventing conflict
@orderBefore If applied, specifies the name of a plugin that must be ordered lower;
used for reasons such as preventing conflict
When directly loading images within plugins, if you check "Exclude unused files" when
carrying out deployment, it is possible that required images will be excluded. Consider the
following images.
- img/pictures folder / image_1.png
- img/system folder / image_2.png
- img/example folder / image_3.png*
You can write the following code that references each of these images.
let b1 = ImageManager.loadPicture("image_1");
let b2 = ImageManager.loadSystem("image_2");
let b3 = ImageManager.loadBitmap("img/example/", "image_3");
* This example specifies an example folder that does not currently exist based on the
plugin's notation.
* @requiredAssets img/pictures/image_1
* @requiredAssets img/system/image_2
* @requiredAssets img/example/image_3
Annotation Definition
@requiredAssets Specifies the path of asset files for which deletion is not allowed
during deployment
If you wish to set the images specified by a game creator using plugin parameters as being
not subject to deletion, you can ensure that as long as the @file and @dir annotations
explained previously have been completed. Please also note that MZ has discontinued the
use of the annotation @require, which featured in the previous version, MV.
The following is a list of the annotations used to ensure files specified in a note are not
subject to "Exclude unused files."
* @noteParam sampleImage
* @noteDir img/sample/
* @noteType file
* @noteData items
Annotation Definition
When creating a Help for each language, provide a language code at the start of the help
comments. For example, the following code is used for Japanese.
/*:ja
* @target MZ
*/
If the language code matches the code that corresponds to the user, that Help can be
used. If they do not match, a Help that does not have a language code assigned, as shown
below, will be used.
/*:
* @target MZ
*/
Licensing
Disclaimer: The goal of this tutorial is not to give legal advice. If your intent is to
license software that does not fall under the definition of open-source software, you
should seek advice from a legal professional. This tutorial nor its contributors are
responsible for the use of any advice provided herein.
A license is, so to speak, a plugin's terms and conditions. Although it is fine if you
determine these terms and conditions yourself, consider the necessity of referring to
wide-ranging cases, such as those involving commercial use or usage as an adult-only
product, by adopting a license that has already been used widespread, you can prevent in
advance any problems or misunderstandings, even among users speaking other languages.
On the other hand, if a plugin has no license or terms and conditions, it is possible that a
user could be infringing upon copyright law just by using it, meaning there is a greater risk
from using the plugin.
Depending on the license, it may be necessary to declare the copyright or clarify legal
provisions or links to legal provisions.