Removing the need for 'this' in Game_Interpreter

If I add a function as follows:


Game_Interpreter.prototype.myFunction = function() {
// do stuff
};


and then I add a Script command to an event:

Code:
Script: myFunction();

it gives me an 'undefined' error.


I have to change it to this to make it work:

Code:
Script: this.myFunction();





Is there any way to remove the need to add this as the subject?
For as long as I study (and understand) no, there isn't
Is there any way to remove the need to add this as the subject?

Only if you don't add myFunction as a method of Game_Interpreter, which is this in Script calls from events.


If you want to call the function with just myFunction(), it would need to be global and I'm not sure if there is any way to declare global functions in plugins... as far as I know all plugins are automatically enclosed into functions to limit their scope. If you can't or don't want to call the function from Game_Interpreter, adding the function to a new namespace / object and calling it from there works:


var MyNamespace = {};

MyNamespace.myFunction = function() {
console.log("Hello World!");
};


Then you would call it with MyNamespace.myFunction() in the Script call or anywhere else.
Seems like none. Even the "classes" in the default js files all call their own functions using this.functionName


or maybe you can try myFunction.call() instead.
That's just as bad though - or maybe worse, because it's out of the ordinary.  ie - it's easier to remember this.xyz than xyz.call() - for someone who doesn't know how to script.


I'm just trying to let whoever uses the plugin to be able to call the function in a script call, without having to remember to put this. in front of it.  Like you can do in Ace.


Thanks for your responses everyone.  Not the answers I was hoping for, but it's good enough to confirm what I already suspected.  Hopefully people will be used to having to use this. with MV script calls.
You can specify certain function as a global scope.


window.myFunction = Game_Interpreter.prototype.myFunction;


If you do that, this code works fine.

Code:
Script: myFunction();
Yeah, someone already said that.  I'd rather not mess with globals though.
Thanks for your responses everyone.  Not the answers I was hoping for, but it's good enough to confirm what I already suspected.  Hopefully people will be used to having to use this. with MV script calls.



Well, with the way the engine is setup, I do think anyone who will fiddle with the scripts/plugins will sooner or later be used to needing to use this.
Plugins are designed so people who aren't programmers don't need to "fiddle" with them at all.  I think it's even less likely now than it was with Ace that non-programmers will need to touch scripts.
You should just explicitly tell them then that the script call should be this.functionName on the instructions.
I do 😉  



I was asking if there's any way to remove the need to have it.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/removing-the-need-for-this-in-game_interpreter.73923/