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.
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.