How to execute a function in a plugin from within rpg maker MV

Hey guys,

Ive created this script that looks if a variable is in use, and if it isnt it stores the event ID inside, and if it is in use, its gonna look at the next variable, etc etc.
Now my question is: How do I place this script inside a plugin, in - I suppose. a function, and let the function execute from within the game. Currently ive got this inside a plugin file:
Code:
testtest = function() {
for(var VV = 21; $gameVariables.value(VV) !== $gameMap.event(this._eventId)._eventId && VV <= 21+39;) {
if($gameVariables.value(VV) === 0 || $gameVariables.value(VV) === $gameMap.event(this._eventId)._eventId) {
$gameVariables.setValue(VV, $gameMap.event(this._eventId)._eventId
)} else {
VV += 1;
}}};
which basically is the script i made + a function.
the function call script I made is:
Code:
this.testtest()
But when I use this, I get the error "TypeError undefined is not a function".
Id love to know what im doing wrong, and maybe some insight on functions in general, as even through ive read tutorials, I have a hard time grasping how they work.

Thanks in advance!
I would declare testtest() function in global namespace (in case it is inside anonymous function, though someone may call it bad practice).

Just make sure that the variable (and function names) are UNIQUE. Then you can use it globally everywhere.

Edit: And do not use this. before calling testtest();
Untitled2.png


Edit2: Also declaration and 1 semicolon is missing. It should be:
Code:
var testtest = function () {
    for (var VV = 21; $gameVariables.value(VV) !== $gameMap.event(this._eventId)._eventId && VV <= 21 + 39;) {
        if ($gameVariables.value(VV) === 0 || $gameVariables.value(VV) === $gameMap.event(this._eventId)._eventId) {
            $gameVariables.setValue(VV, $gameMap.event(this._eventId)._eventId);
        } else {
            VV += 1;
        }
    }
};
@OcRam Thanks for the pointers dude! After I corrected what you said, I also got a new error about .eventId being undefined, so I tried something someone on here had told me previously, and took a gamble to see if it worked and ended up with:

Code:
var testtest;
var VV;
testtest = function($this) {
    for(VV = 21; $gameVariables.value(VV) !== $gameMap.event($this._eventId)._eventId && VV <= 21+39;) {
        if($gameVariables.value(VV) === 0 || $gameVariables.value(VV) === $gameMap.event($this._eventId)._eventId) {
            $gameVariables.setValue(VV, $gameMap.event($this._eventId)._eventId);
        } else {
            VV += 1;
        }
    }
};

Code:
testtest(this);
And that worked, luckily!

I also declared the VV variable at the start, because i read somewhere that it is supposed to be a good practice or something. Although i dont see much point in it personally.
I also declared the VV variable at the start, because i read somewhere that it is supposed to be a good practice or something. Although i dont see much point in it personally.
Glad to hear that you got it working!

Why variables should ALWAYS be declared? (and "use strict" is highly recommended):
  1. You can avoid unexpected results
  2. Your code runs faster and consumes less memory

And "use strict" has even more benefits (addition to above pointers) below is quote from w3school:
Strict mode makes it easier to write "secure" JavaScript.

Strict mode changes previously accepted "bad syntax" into real errors.

As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.
@OcRam hmm interesting- also about the strict mode, which ive never heard of before. It makes sense, whats the point of code that has mistakes, small as they may be.
I've created a plugin with this simple function:
Code:
(function() {
    var logTest = function() {
        console.log("test!");
    }
})();

In an event I put inside the Script dialog:
logTest();

Run the game and... "logTest is not a function".

If I put the global function outside the IIFE doesn't works either.
The purpose of an IIFE is to hide the scope of what's inside from what's outside. If you want something to be accessible from outside then you definitely don't want to use an IIFE.

If you define a function globally, it should be able to be called from anywhere. Can you share the code you wrote for when you say "If I put the global function outside the IIFE doesn't works either"?

Original thread: https://forums.rpgmakerweb.com/threads/how-to-execute-a-function-in-a-plugin-from-within-rpg-maker-mv.94971/