Removing the need for 'this' in Game_Interpreter

Status
Not open for further replies.

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,715
First Language
English
Primarily Uses
RMMV
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?
 

Vito The Boss

Someone better than you, or will become
Veteran
Joined
Nov 1, 2016
Messages
175
Reaction score
64
First Language
Italiano
Primarily Uses
For as long as I study (and understand) no, there isn't
 

LadyBaskerville

Hell-poodle
Veteran
Joined
Sep 12, 2016
Messages
645
Reaction score
524
First Language
German
Primarily Uses
RMMV
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.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,715
First Language
English
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

biud436

Veteran
Veteran
Joined
Sep 28, 2014
Messages
172
Reaction score
180
First Language
Korean
Primarily Uses
RMMV
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();
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,715
First Language
English
Primarily Uses
RMMV
Yeah, someone already said that.  I'd rather not mess with globals though.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,715
First Language
English
Primarily Uses
RMMV
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.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
You should just explicitly tell them then that the script call should be this.functionName on the instructions.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,715
First Language
English
Primarily Uses
RMMV
I do ;)  



I was asking if there's any way to remove the need to have it.
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,381
Reaction score
8,537
First Language
English
Primarily Uses
RMMV
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.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

On my journey of character rework: I had this character, she was meant to be just a princess that joins your party. And at long term she was just uninteresting... So I tweaked her to be a rebel agaisn't the royalty before meeting up with the party.

Quick tip for any other ametuer pixel artists! When trying to create a colour palette, enabling Antialiasing can speed up the process of creating different shades! Just place your lightest colour and your darkest colour next to each other, select both pixels, and stretch it out!
Revolutionizing the JRPG Industry: Knocking on Doors.

Take that, murderhobos.
Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.

Forum statistics

Threads
106,054
Messages
1,018,578
Members
137,843
Latest member
Betwixt000
Top