MZ Break loop in script

I want to break a loop from inside my Plugin using command113(), but it doesnt work. It works however when i run it in a script using this.command113(). I logged, what "this" is and its the Game_Interpreter, but i cant use the command with Game_Interpreter.command113(). Why?
because a lot of commands are context-sensitive, and without seeing your entire code as well as getting a description of what it is supposed to do we can't tell you.

just for example - there are two different game interpreter instances, one for the map events and one for troop events.
(And every Parallel event has its own interpreter, too.)
Well there isnt much code to show. All im doing is a loop which calls a plugin command that should end the loop. I am also calling everything from the map.
My Event looks like this:
1690139415991.png

And this is how my Plugin looks:
1690139442974.png
You've used an arrow function, which does not have its own this value:
Also, the first input to registerCommand should be a string. You've used a variable but haven't shown what value you've stored in that variable.

Assuming your plugin is named Test.js, try this:
JavaScript:
PluginManager.registerCommand("Test", "End", function(args) {
    this.command113();
});
You've used an arrow function, which does not have its own this value:
Also, the first input to registerCommand should be a string. You've used a variable but haven't shown what value you've stored in that variable.

Assuming your plugin is named Test.js, try this:
JavaScript:
PluginManager.registerCommand("Test", "End", function(args) {
    this.command113();
});
Bruh... thanks for the answer it works now. But i cant call the interpreter directly or can i?
You can if you actually reference it. Just saying Game_Interpreter is the name of the class, not an instance of an interpreter in the game.

$gameMap._interpreter is the interpreter for the current map.
$gameTroop._interpreter is one for troop events.

You just have to figure out how to correctly reference the interpreter that you need - and from inside an event, that's most easily this
Hmm i tried using $gameMap._interpreter for that, but it didnt work. Might have to test that again. I tried to find it in the files, but vs code couldnt find it. Are they defined somewhere?
i tried using $gameMap._interpreter for that, but it didnt work.
and where and when did you try it?
as said that is the interpreter for the current map and it will not work if you want to do something outside that map or when the map does not exist (on the title screen for example)

It comes back to your original problem: global references are restricted for security reasons, and commands other than "this" will not always be accessable anywhere.
So your first step should always be "where and in which instance and in which context does my code run?", and only after you know that you can check what is available there.
But i cant call the interpreter directly or can i?
You can reference the interpreter as this in a plugin command, because a plugin command is run in the context of the calling interpreter. Turan mentioned ways to reference the main map/troop interpreters globally, but that is not usually necessary in good code.

Hmm i tried using $gameMap._interpreter for that, but it didnt work.
  • $gameMap is an instance of the Game_Map class.
  • $gameMap._interpreter is an instance of the Game_Interpreter class on that map instance.
It is defined in the core scripts, but the map may not have been initialised yet. If it didn't work, then like Andar says: when, where, and why are you trying to reference it? 😅

I think you may save a lot of time in the long run if you read up on some of the basics, such as scope, classes vs instances, and inheritance. In case you're interested, here are a couple of links (or you can find your own):
Part of this comes back to the very top - why are you trying to do this via script?

You're doing in a Plugin Command, which means you're already in an event. Why on earth aren't you simply using the Break Loop event command?

You can do whatever you want in your game, but it just doesn't make sense to use code when existing event commands just work.

Original thread: https://forums.rpgmakerweb.com/threads/break-loop-in-script.159628/