Best way to turn my code into a plugin + how to use plugin command

Hey guys,

I made a script that uses the common events name, instead of the ID, and now id like to know what the best method is of using this script as a plugin, and how I can implement a plugin command for the common events "name" for example. I also wouldnt mind some general plugin tips, and/or pointers of making my code even more efficient.
Code:
◆Script:var NAME = "ha", Counter;
:      :for(Counter = 1; Counter < $dataCommonEvents.length; Counter++) {
:      :  if ($dataCommonEvents[Counter].name === NAME) {
:      :    var commonEvent = $dataCommonEvents[Counter];
:      :    if (commonEvent) {
:      :        var eventId = this.isOnCurrentMap() ? this._eventId : 0;
:      :        this.setupChild(commonEvent.list, eventId);
:      :    }
:      :    break;
:      :  }
:      :};
To put your script into a plugin, simply create a .js file and copy the script to it.
For your plugin to actually run, you need to inject its code somewhere. This is done by extending one of RMMV's core scripts.

In your case, it seems like you want your plugin to be used through a "Plugin Command".
The code for that is quite simple:
Code:
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command !== 'myCommand') return;

    // do stuff
};

We basically overwrite Game_Interpreter.prototype.pluginCommand, so we can do our stuff. The problem with overwriting existing code, is that the existing code is now lost.
To prevent that, we keep a copy of the existing code (first line), then call it. This is called "extending" as opposed to "overwriting".
You can call the old code wherever you want, but I chose to call it first because there's no real reason not to.

In the if, I check for "myCommand". If it's not, I return, so the rest of the code doesn't get executed.
I personally like writing it that way, because I don't like indenting my code when I don't have to. It's also somewhat easier to read in my opinion.

Alternatively, I could've also used:
Code:
if (command === "myCommand")
    doStuff();
... where doStuff is a function that handles my command. This approach can be useful if you're implementing multiple plugin commands within the same plugin, like this:
Code:
if (command === "command1")
    doStuff1();

if (command === "command2")
    doStuff2();

if (command === "specialCommand")
    doSpecialStuff();

Now that you know how to use command, you're probably wondering what args does.

When you call a plugin command like this:
myCommand Alice Bob

... then command will be "myCommand", and args will be ["Alice", "Bob"].
In other words: args[0] will be "Alice", and args[1] will be "Bob".

There is no limit to how many items can be in args.

Spoiler: Code improvement
It's better to define counter variables where they're used, like here:
Code:
for (var i = 1; i < bla; i++)
Also use let instead of var. The reason is that var is valid within the entire function it was defined in, while let is only valid inside the code block it was defined in.

For instance:
Code:
function f() {
    for (var i = 0; i < 10; i++) {
        // "i" is valid here
    }
    // "i" is also valid here

    for (let j = 0; j < 10; j++) {
        // "j" is valid here
    }
    // "j" is no longer valid here
}
It's more efficient because the memory can be freed sooner. It's ok to use var if you prefer, though, because let is quite a bit harder to read than var, in my opinion.


For clarity's sake, avoid defining multiple unrelated variables, especially if they're not the same type.
Code:
var surname, familyName;
... is ok, because they're related, and they're the same type (strings). But...
Code:
var name, age;
... is bad, because they're not the same type; "name" is a string, "age" is an integer.

It's just really hard to read, especially when you have lots of them, like here:
Code:
var name, age, treeNode, oldIndex, newIndex, depth, isFound;


Next, for counter variables inside for loops, using the name "i" is usually better than "Counter".
The reason is because "i" is faster to type than "Counter", and because most programmers are used to for (i = 0; ...) and similar. In both cases, we don't know exactly what "i" or "Counter" do, but since we're used to seeing "i", why not use it?

One example where it would make sense to use a different name would be:
Code:
for (let studentId = 0; studentId < students.length; studentId++) {
    let student = students[studentId];
    // do something
}

Another example, and quite classic in games, would be "x" and "y". They usually represent coordinates in 2D space (3D with "z"), so when you see "x" or "y" you have a pretty good idea of what they're used for.
Code:
for (let x = 0; x < width; x++) {
    for (let y = 0; y < height; y++) {
        map[x][y] = 0;
    }
}


Next, I would split your script into 2 functions, like this:
Code:
function findCommonEvent(eventName) {
    for (let i = 0; i < $dataCommonEvents.length; i++) {
        let event = $dataCommonEvents[i];
        if (event.name === eventName)
            return event;
    }
    return null; // Event not found
}

function doStuff(eventName) { // Not sure what your code does, so I named it "doStuff" as a placeholder
    let event = findCommonEvent(eventName);
    if (event) {
        // do stuff
    }
}

This way, your code is cleaner. It's also easier to find bugs. If doStuff ever misbehaves, it's easy to tell if the bug lies in findCommonEvent or not. You can also reuse findCommonEvent for other, unrelated functions.
The less stuff a single function does, the easier it is to read. If you ever look at your code after a break of several weeks, you don't have to spend quite as long to understand your code, especially if your functions have meaningful names.


By the way, there's no need to write code for findCommonEvent. Arrays already have a function for that:
Code:
let name = "bla";

let doesEventNameMatch(event) {
    return event.name === name;
}

let event = $dataCommonEvents.find(doesEventNameMatch);

If you only need that function once, you can also make it anonymous, like this:
Code:
let name = "bla";
let event = $dataCommonEvents.find(function(event) { return event.name === name; });
@Nolonar Wow thats really thorough, incredibly insightful. Took me a while to comprehend the logic, still dont completely do, but getting there. And its also exactly what Ive asked for, and solves my problem. And these tips you've given will serve me well for sure!

I have one problem, and i know its not the original topic, but while im learning; I just cant seem to get these "find()" to work in rpg maker mv, Ive tried many different methods and ways, but i keep getting an error that 'undefined is not a function'. I did also try the "find()" in some coding thingy on a information website, and it worked there. Any idea whats wrong? Or maybe you could post a bit of code what should work and where i need to place it, where I can analyse what Im doing wrong.
find() is an array method, so it has to be used on arrays.

More information here.

It was introduced in the 2015 specification of ECMAScript, so it might require MV v1.6.0+ to use it. Not all ECMAScript2015 features are supported in older versions of MV.
@Aloe Guvner Aye thats the site where I had tested it, where it seemed to work fine. Since i still us mv 1.5.2, It would make sense that it wont work. Ohwell

Original thread: https://forums.rpgmakerweb.com/threads/best-way-to-turn-my-code-into-a-plugin-how-to-use-plugin-command.95523/