MV Interpreter's wait method within custom function

To the point...

I want to know how to properly tell interpreter to wait for another method within function?
Example:
JavaScript:
var delay = 120; //2 seconds
$gameMessage.add(message1);
$gameMap._interpreter.setWaitMode('message');
$gameMap._interpreter.wait(delay);
// Basicly at this point game should (must) wait 2 seconds (120 frames) for next message AFTER user's input (Action key)
$gameMessage.add('message2');
@Soulrender

Very good question. I really hope to see responses from the folks experienced with the architecture on either the solution or the next best workaround.
unfortunately wait doesn't function in the way its name suggests, which is what makes this idea complicated.
I can't give you the code, but I can explain to you what really happens inside the engine.


There is a main game loop that contains a lot of objects like the events and display update functions and more. This loop is processed once per frame.
the "wait" command now basically tells that loop "skip this object for x frames".

In the case of an action button event, that looks like a wait.
In the case of a parallel process, this skipping results in less processing and less lag.

The problem begins if you want to have the "wait" based on a different object in the game loop instead of its own processing, because that would require global access instead of instanced access, or some difficult handshake or something like that.
And that is something where I only know the general theory but not the exact implementation of RM's game loop, so I can't help.
I'll paste this here for others:
Spoiler: interpreter waiting + inserting commands
The effect of this:
JavaScript:
$gameMessage.add("text1");
this.setWaitMode("message");
this.wait(120);
$gameMessage.add("text2");
this.setWaitMode("message");
...is to add 2 lines of text to the message, set the interpreter's wait mode to "message" (twice, 2nd time does nothing), then start waiting for 120 frames while the message is showing. Instead it should be 3 script calls, one after the other:
JavaScript:
$gameMessage.add("text1");
this.setWaitMode("message");
JavaScript:
this.wait(120);
JavaScript:
$gameMessage.add("text2");
this.setWaitMode("message");
This gives the interpreter appropriate opportunities to check its wait mode and/or wait time.

For your situation, you may be better off inserting commands into the interpreter instead. This sort of thing can be used in a Script command to insert commands into the current interpreter:
JavaScript:
var i = this._indent;
var pre = this._list.slice(0, this._index + 1);
var post = this._list.slice(this._index + 1);
this._list = pre.concat([
 { code: 101, indent: i, parameters: ["Actor1",0,0,0] },
 { code: 401, indent: i, parameters: ["text1"] },
 { code: 230, indent: i, parameters: [120] },
 { code: 101, indent: i, parameters: ["Actor2",1,0,0] },
 { code: 401, indent: i, parameters: ["text2"] }
], post);   // new array, do not edit original
Code 101 is Show Text (faceset name, faceset index, background, position), 401 is Show Text (actual text), and 230 is Wait.
😮‍💨
This is the approach I'd use, but I imagine there are alternatives.
@caethyril - this code works well and that's a very good starting point for solving totally my main problem with MV, however it has one flaw, "Script..." event is limited to 12 lines, but... I'll check something you mentioned somewhere else.

Edit:\\ Huh 😳. Strangely enough... It's working, but... um... Naah, nevermind, it's working, so I won't will dig it further. However, I'm keen to see other options.
@Soulrender even if scriptcall is max 11-12 lines, you can set all it behind each other,
while the lenght is limited so far I know. so you get basicly 1-3 lines in total.
example is

if (12345678913dksnklgnsadkgnlsnglnsalnghlsfnhkgnsfklhnkldfnhnadlnkadk)
else
dmsdkgklsgksfkgskghkfkhjkdlhklsndkl;hn;lasdnhl;nasdhlnhikn etc etc (with actual code)

so you scriptcall has 2 lines, but you can save the code on a txt file for easier edit,
and paste if needed 🙂

so you never use the full 12 lines this way, short codes are easier to read, very long codes,
can be copy paste, because I use a 50 liner in a scriptcall to 3 lines
(if true, do this
esle
do what if it's false
but paste the code after each other if the code is longer than 12 lines ^^
@ShadowDragon, yeah, but I already tested what @caethyril suggested i his post as a method in a custom class in ES6 standard. Only one thing doesn't go off my mind:
$myClass.testRun() wouldn't run at all with script call, but $myClass.testRun.call(this) executes @caethyril's code without a scratch.

Edit: It looks like there is no other way than translating classic script calls to number codes for interpreter. Great.
$myClass.testRun() wouldn't run at all with script call, but $myClass.testRun.call(this) executes @caethyril's code without a scratch.
That is because of scope/context. The this keyword references the current context: in a Script command, that is the current Game_Interpreter. The example I gave assumes that this points to the relevant interpreter, i.e. it assumes that it will run in a Game_Interpreter context. The call method invokes a function with a specific context, i.e.
  • $myClass.testRun() will use a this value of $myClass.
  • $myClass.testRun.call(this) will run the function in the current context.
You might have seen apply and/or bind before: these are similar to call, they are used to change the context in which a function runs.

Relevant articles:

Edit: It looks like there is no other way than translating classic script calls to number codes for interpreter.
If implementing this as a plugin, you could write your input data in a "nice" format, then have a function translate that into event commands. Regardless, you might find the Event Code sheet here helpful:
It's for MZ but I think the command codes/parameters are mostly the same for MV~
@caethyril your post with the interpreter commands and their human alternatives turned out to be a lifesaver. It answered a question I didn't even know I was going to have 🙂
@Sepheyer, you can always count on @caethyril.

Original thread: https://forums.rpgmakerweb.com/threads/interpreters-wait-method-within-custom-function.140689/