RMMZ Help with scope of plugin commands

RinTheWanderer

Villager
Member
Joined
Aug 23, 2020
Messages
8
Reaction score
6
First Language
English
Primarily Uses
RMMZ
EDIT: SIgh. Turns out arrow functions bind the context they were created in and that overrides .bind. Swapping it to normal function notation and it works fine. Hopefully this is useful to other people at least.

I've about 99.9% finished a plugin to have events set a moveroute to either a specific tile or a specific event with pathfinding. All of the code works fine, if I call it from a script it works flawlessly. My issue is, I cannot get it work through a plugin command. My registerCommand looks like this.

PluginManager.registerCommand('RINMoveTo', "setRouteToTile", args => {
console.log(this);
this.setRouteToTile(args.movingEvent, args.destination, args.waitMode);
});

For some reason, "this" is the game window which throws an error of course and I don't understand why or how to make it be the interpreter like it should be. setRouteToTile is a function I added to Game_Interpreter and if I call it from script it works fine.

In the plugin manager code it handles calling a command like this.

PluginManager.callCommand = function(self, pluginName, commandName, args) {
const key = pluginName + ":" + commandName;
const func = this._commands[key];
if (typeof func === "function") {
func.bind(self)(args);
}
};

If I use console.log(self); in there, it shows that self is a game_interpreter just like it should be but somehow self isn't binding to the function. I've also logged func just to be sure and it is the correct function. I feel like the code is telling me 2+2 == 5 and I just have no idea why the scope of the bound function is the game window and not the interpreter.
 
Last edited:

tvorimvsevmeste

Villager
Member
Joined
Nov 11, 2015
Messages
22
Reaction score
3
First Language
русский
I am very interesting in your pathfinding plugin! I will look forward to it!
What algorithm do you use? A* pathfinding ? Or another?
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,938
Reaction score
1,386
First Language
EN
Primarily Uses
RMMZ
Technically it's not being overridden: arrow functions simply do not possess a specific execution context, i.e. their this value is dependent upon where they are defined. There are some other differences, details here:
The default pathfinding algorithm for MV/MZ is a short-range (12 tiles) basic A* implementation. Without plugins/scripting it's only used for player touch movement.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Yesterday I made my first step towards eating more healthily.
I saw candy on discount and did not buy it.
"They yearn for what they fear for."
I always told my DA fans how much I hate slot machines. They're fine in games as a risk-and-reward system. But when you're spending REAL MONEY in a Vegas casino to try and hit the jackpot (which very, very few people will), it can really hurt your budget. Gambling is a bad habit, and I don't like wasting my money on a slim chance. Go to Vegas for the experience, not the jackpot.
Took the kids to a corn maze. They gave us a map and had lights at certain points in the maze. Not overwhelming... or underwhelming... just... whelming.
Okay, vacuuming fruit flies out of the air is surprisingly effective.

Forum statistics

Threads
104,393
Messages
1,006,073
Members
135,927
Latest member
ecelebrithytrend
Top