function seems to work through console.log?

hey guys,

Im currently experimenting with functions in rpg maker mv, and ive stumbled upon something that i dont understand.
Code:
◆Script:"use strict"
:      :var labelName = "6";
:      :for (var i = 0; i < this._list.length; i++) {
:      :  var command = this._list[i];
:      :  if (command.code === 118 && command.parameters[0] == labelName) {
:      :    this.jumpTo(i);
:      :  }
:      :};
◆Script:console.log(this.command115())
◆Label:5
◆Play SE:Blow3 (90, 100, 0)
the console.log script, which is for exit event processing(which returns true btw) seems to still end the event, even when its in console.log - While i thought that things in console.log() didnt have any impact besides showing the contents in console log? Could someone explain to me how this is possible?
Just for ease of reference, let's have a look at the definition of command115:
Code:
// Exit Event Processing
Game_Interpreter.prototype.command115 = function() {
    this._index = this._list.length;
    return true;
};
It's primarily a method, in that it's used to produce an effect (by changing a variable). The _list property of Game_Interpreter is a list of all event commands in the currently-running event; _index keeps track of which one we're on at the moment. So what this command does is say "skip any further event commands in the list".

Now to your line of code:
<snip>
Code:
◆Script:console.log(this.command115())
Brackets work here like they do anywhere else: the stuff in the brackets is evaluated first. So the code runs command115, which happens to also return the value true. Thus the line becomes:
Code:
console.log(true);
So it prints true to the console. Then the interpreter goes to evaluate the next line, sees it's reached the end of the event (thanks to command115) and stops. 😮‍💨

Typically console.log is used with static values and variables or "proper" functions, i.e. functions that just return a value and don't "do stuff" besides that, which is probably why you've only just encountered this kind of issue. =)


Edit: try this instead if you want to see the code behind the command:
Code:
console.log(this.command115);
No brackets after the command115 means it's treated as an object that doesn't require evaluation; this means the console.log will run immediately. The argument (this.command115) is implicitly converted into a [multi-line] string and we see the code appear in the console. Success! 😂
Easy.
console.log(this.command115())
You had these brackets there, so you called that function.
Remove them and it should work.
@caethyril I think I get how it works now, thanks alot!

Original thread: https://forums.rpgmakerweb.com/threads/function-seems-to-work-through-console-log.95471/