Creating a Custom Plugin

Return to Index

 This guide will help you with the basics of how to create a plugin for SRPG Studio.

## Requirements

To create a plugin, you will need two things:

* Basic knowledge about programming: this software uses Javascript, so you will need to at least know that language works. While knowing about Javascript is ideal, if you know about other object-oriented language, you should be able to adapt to Javascript.

* A text editor: you can use something like Notepad++, or you can use a more advanced software like Visual Studio Code.

## Creating the Plugin

### Editing the original scripts

Every project has a folder called Script, where all the Javascript files reside. If you modify one of those files, you can change how the game works. But this is not the right way to create a plugin: those files will reset to default each time the software updates, and even if that wasn't the case, it would be difficult to keep track of each change.

For this reason, every change should have its own separate file. This is why the Plugin folder exists. That is the folder where you can put every custom plugin.

### Creating a new file

The first step to create a plugin is to make a new file in the Plugin folder of your project. Every custom plugin must be inside that folder, although you can put the files in sub-folders too. Note that each file must have the .js type.

### Overwriting Functions

Now that a file has been made, we can start writing some code. To do this, we have to overwrite a function that already exists in the original files, making a new version of said function.

Let's use a small function as an example on how to do this. The next function shown is the function called getHit, and is part of the object in the variable AbilityCalculator. It is located in line 19 of singleton-calculator.js

(Note: every line that begins with '//' is just a comment about the function, it doesn't count as code.)

```
var AbilityCalculator = {

```

```
// ...

```

```
  getHit: function(unit, weapon) {

```

```
        // Hit rate formula. Weapon hit rate + (Ski * 3)

```

```
        return weapon.getHit() + (RealBonus.getSki(unit) * 3);

```

```
  },

```

```
// ...

```

```
};

```

As you can see, the functions will call the getHit() function of the weapon of the unit, get the Skill value of the unit multiplied by 3, and finally return the sum of those numbers.

Now that we can understand the function, we can change it. For example, let's make the function return the weapon hit plus the unit's skill multiplied by 5 instead of 3. It will now look like this:

```
var AbilityCalculator = {

```

```
// ...

```

```
  getHit: function(unit, weapon) {

```

```
        return weapon.getHit() + (RealBonus.getSki(unit) * 5);

```

```
  },

```

```
// ...

```

```
};

```

The above function would work if we edit the original file, but if we are writing a new file, we need to change how the function is declared. First, the function need to be called using the name of the object it belongs. In this case, the object is called AbilityCalculator, so instead of calling it getHit, we will call it AbilityCalculator.getHit. The other change, while it may seem very small, is importan to make it work. You have to change the ':' between the name of the function and the word function and replace it for a '='. This way, the function would look like this.

```
AbilityCalculator.getHit = function(unit, weapon) {

```

```
        return weapon.getHit() + (RealBonus.getSki(unit) * 5);

```

```
},

```

Now, if a file with this code is inside the Plugin folder, it will change how the hit rate formula works.

### Calling the overwritten function

Some times, instead of completely overwriting the function, you would like to add some additional code without modifying the original script. For example, let's grab the same getHit function, but instead of modifying it, we will use the same formula and add the Luck stat of the unit to the result.

```
var oldHitFunction = AbilityCalculator.getHit;

```

```
AbilityCalculator.getHit = function(unit, weapon) {

```

```
        var hit = oldHitFunction.call(this, unit, weapon);

```

```
        return hit + RealBonus.getLuk(unit);

```

```
},

```

Let's see this code line by line. First, we create a new variable called oldHitFunction, that has the old function as the value. We know is the old function and not the new one because this variable is defined before we define the new function. Then, in the first line of the new function, we made a variable called hit that calls the function inside the oldHitFunction. This works by using the call function with all the arguments of the new function, plus the 'this' value as the first argument. Once the call function is called, we have inside the hit variable the value of the original function, so now we can modify that value (in this case, by adding the Luck stat of the unit) and return the modified value as the new result.

### Compatibility Issues

When reading the script files, the software will give priority to the files inside the Plugin folder over those in the Script folder, so all custom plugins will overwrite the default files. But if there are two files in the Plugin folder that have the same function, only one of them will work. This is a very important when making a new plugin, so you should never copy and paste the entire file when trying to edit a single function. Only write the function that will be edited, otherwise you run the risk of overwriting a custom function from another plugin with the default script.

A way to partially avoid this clash of plugins is to call the overwriting function as explained above. For example, if we have two plugins, one overwrites the getHit function to add the Luck stat and the other does the same but add Speed instead. In this case there is a clash, so let's say that the software ends up grabbing the function that add the Luck stat. What happened is that the Speed version overwrote the default function, and then the Luck version overwrote the Speed version. That is a problem, so the solution in this case is to make both functions use the call function. This way, the Luck version, when using the call function, will call the Speed version and then the Speed version will call the default one. Note that if one of them doesn't have the call function, this chain of calls will break.

In conclusion, if possible, always use the call function to improve the compatibility of your plugin with others made by you or other people.