[RMMV] For loops in event scripts

Hello!

I've noticed that 'for' loops don't seem to work in event script blocks. It doesn't throw an error, it just freezes the game (forcing me to forcibly kill the window). This leads me to think it's stuck in some kind of infinite loop.

I've tried to use the following (which involves Yanfly's Self Vars/Switches plugin, but I don't think the problem is with the plugin):

for (var myCounter = 1; myCounter < 5; myCounter++) {
this.setSelfSwitchValue(1, myCounter, 1, true);
}

I tried using 'let' in the iterator declaration, thinking it was some kind of weird scope issue, but the game said 'let' wasn't supported in my version (1.5.2; is 'let' supported in 1.6?).

Can one just not use loops in event script blocks, or is there some other kind of mojo I need to invoke to get it to work?

Thank you!
Just for clarity - are you using that as a complete 'stand-alone' script call within the event, or is it a script call inside the 'Loop' command on the Event Command Menu?
It's crashing because you have an error in your code. The same thing would happen if you removed the for loop completely and just set a self switch using numbers instead of variables.

There's no such thing as this.setSelfSwitchValue. The correct line would be
Code:
$gameSelfSwitches.setValue([1, myCounter, 'A'], true);
It's crashing because you have an error in your code. The same thing would happen if you removed the for loop completely and just set a self switch using numbers instead of variables.

There's no such thing as this.setSelfSwitchValue. The correct line would be
Code:
$gameSelfSwitches.setValue([1, myCounter, 'A'], true);

It's a function from Yanfly's self switches and variables plugin. It doesn't affect the game's built-in self switches; it allows you to create a different set of switches (as many as you want, and even variables!) that are local to the event by following certain conventions when naming a subset of switches and variables. I run that command all the time, just not in loops.

Just to remove the potential complication of the plugin, I tried running a for loop that modified some $gameVariables, and it worked. I guess I should have done that in the first place, I was just convinced it wasn't the plugin! Maybe the loop changes something about the context, causing 'this' not to mean the same thing? Though you'd think that would throw an error. I guess I'll have to experiment. Oh, well. Sorry for the bother!

By the way, the script call wasn't in one of the editor's "loop" blocks.
So is "this" referring to the event, or to the interpreter? Script calls get called in the interpreter context, so if 'this' is meant to refer to the event, use $gameMap.event(this._eventId) instead of just this.

If that doesn't work, please post a link to the plugin.
Just in case this is of some relevance to someone later on, moving the plugin higher up the plugin list (as Yanfly advises) seems to have cleared up the problem. I guess plugin order really can be important!

Original thread: https://forums.rpgmakerweb.com/threads/rmmv-for-loops-in-event-scripts.95644/