MZ How to do simple dialogues/show text messages in plugin script?

Hello,
I'm trying to export all of the storytelling to plugin file because I find the event editor to be extremely tedious and annoying to use. I'm a software developer (so I know my way around js) with a background in Ren'Py and I'd love to edit the dialogues in a similar way.

What I find difficult is executing the normal, typical dialogue like such:

JavaScript:
function scenario_1(){
    showText(mc, "Huh, so that's what this is all about...");
    showText(mark, "Wait until you see the main hall.");
}

How can I implement the showText function to work as intended? I'm guessing it has to be done with callbacks (when text is dismissed show the next text message etc.) but I have no idea how to do that.

To my understanding, the interpreter doesn't have any 'addCommand' function and calling interpreter.command101(params) directly would be problematic because it already assumes the text command has already been issued and the method just tweaks the box a bit to fit the text passed previously and elsewhere.

Initially I tried to do something like this:

JavaScript:
$gameMessage.add('my text');
interpreter.setWaitMode('message');

But the interpreter doesn't stop the script interpretation afaik, just the event stack interpretation. That means that the consecutive dialogue messages would have to be put in different events and that's exactly the opposite of what I'd like to do.

I'm new to RPGM so I don't understand much of it. If you have any recommendations or advices then please, go ahead.

PS.
So far I've been accessing the interpreter by passing it as an argument in Event->Script->PluginManager.callCommand and I feel like there's an easier way of doing it.
Don't know too much about this stuff, but it works for me if I just put it like it's in the script call list:

function scenario_1(){
$gameMessage.add('my text');
$gameMessage.newPage();
$gameMessage.add('my text 2');
$gameMessage.newPage();
$gameMessage.add('my text 3');
$gameMap._interpreter.setWaitMode('message');
}

EDIT: Ok, not all that stuff was in the script call list
Don't know too much about this stuff, but it works for me if I just put it like it's in the script call list:

function scenario_1(){
$gameMessage.add('my text');
$gameMessage.newPage();
$gameMessage.add('my text 2');
$gameMessage.newPage();
$gameMessage.add('my text 3');
$gameMap._interpreter.setWaitMode('message');
}

EDIT: Ok, not all that stuff was in the script call list
Thanks for the reply. The problem I have with this approach is the moment I choose to insert a choice (like a simple yes/no) the solution falls apart.

You can't put a choice after the messages because the interpreter doesn't stop the code execution. I would have to wait until the player dimisses the last message and only then tell the interpreter to show the choices (correct me if I'm wrong). However there is no interpreter hook like interpreter.onCommandProcessed(callback) or sth to that effect.

Thanks for the $gameMessage.newPage() though. I haven't heard about method - it sounds useful.

In the meantime I've managed to tinker a workaround. I'll post it here if stands the test of time. It's still a workaround though so I'd like to hear other ideas.
You could see what is inside the data json for the event and go from there (in the key 'list' is the array of objects with code, indent, and parameters). Throw it into a function and run it:
JavaScript:
function runTextCode(character) {
    const list = [
        {"code":101,"indent":0,"parameters":["",0,0,2]},
        {"code":401,"indent":0,"parameters":["Test 1"]},
        {"code":101,"indent":0,"parameters":["",0,0,2]},
        {"code":401,"indent":0,"parameters":["Test 2"]},
        {"code":102,"indent":0,"parameters":[["Yes","No"],1,0,2,0]},
        {"code":402,"indent":0,"parameters":[0,"Yes"]},
        {"code":101,"indent":1,"parameters":["",0,0,2]},
        {"code":401,"indent":1,"parameters":["Choice 1"]},
        {"code":0,"indent":1,"parameters":[]},
        {"code":402,"indent":0,"parameters":[1,"No"]},
        {"code":101,"indent":1,"parameters":["",0,0,2]},
        {"code":401,"indent":1,"parameters":["Choice 2"]},
        {"code":0,"indent":1,"parameters":[]},
        {"code":404,"indent":0,"parameters":[]},
        {"code":0,"indent":0,"parameters":[]}
    ];
    // replace the current event's/character's list of commands with this list
    // immediately start the event's/character's new list
    // changes stay on the scene until it is changed (menu, map change, etc.) - reverts to original
    character.page().list = list;
    character.start();
}
s1.png

Here is the eventing equivalent:
s2.png

The interpreter runs each event command every frame updated. Your script call is run as one eval() so everything inside it runs within that frame. Multiple $gameMessage.add() functions are joined together - the .newPage() function separates those pages only and does not take into account choices.

The way this is setup is the same way code is written in the editor. The editor has commands that are evaluated into commands (in game interpreter) and those are executed by index in the list and updated every frame.
Ah yeah, if you want 'show choices' too (anywhere but the end), without using another script box, then that simple solution I posted doesn't work.

@bass2yang solution looks good. Since you said you know javascript, I guess it's not even hard to automate the building of that list, so you don't have to copy/paste the codes all the time.
You could see what is inside the data json for the event and go from there (in the key 'list' is the array of objects with code, indent, and parameters). Throw it into a function and run it:
JavaScript:
function runTextCode(character) {
    const list = [
        {"code":101,"indent":0,"parameters":["",0,0,2]},
        {"code":401,"indent":0,"parameters":["Test 1"]},
        {"code":101,"indent":0,"parameters":["",0,0,2]},
        {"code":401,"indent":0,"parameters":["Test 2"]},
        {"code":102,"indent":0,"parameters":[["Yes","No"],1,0,2,0]},
        {"code":402,"indent":0,"parameters":[0,"Yes"]},
        {"code":101,"indent":1,"parameters":["",0,0,2]},
        {"code":401,"indent":1,"parameters":["Choice 1"]},
        {"code":0,"indent":1,"parameters":[]},
        {"code":402,"indent":0,"parameters":[1,"No"]},
        {"code":101,"indent":1,"parameters":["",0,0,2]},
        {"code":401,"indent":1,"parameters":["Choice 2"]},
        {"code":0,"indent":1,"parameters":[]},
        {"code":404,"indent":0,"parameters":[]},
        {"code":0,"indent":0,"parameters":[]}
    ];
    // replace the current event's/character's list of commands with this list
    // immediately start the event's/character's new list
    // changes stay on the scene until it is changed (menu, map change, etc.) - reverts to original
    character.page().list = list;
    character.start();
}
View attachment 342516
Here is the eventing equivalent:
View attachment 342517
The interpreter runs each event command every frame updated. Your script call is run as one eval() so everything inside it runs within that frame. Multiple $gameMessage.add() functions are joined together - the .newPage() function separates those pages only and does not take into account choices.

The way this is setup is the same way code is written in the editor. The editor has commands that are evaluated into commands (in game interpreter) and those are executed by index in the list and updated every frame.
Holy , this looks extremely informative and helpful. I'll check out your answer in more detail when I'll be back at work again and I'll give you a feedback!
Here is something I've worked on in the past. I didn't like that I had to deal with the editor's loops, if statements, wait timings for script calls, etc. So, I wrote a function that parses the information from an array where 0 is the command, 1 is the indent, and 2 is the parameters.

Not bad, but also, quite verbose in approach - I only use it when I want to put everything in one script call but still have it interpret each line of code in different frames - it also works great outside the editor when writing functions where I want the game interpreter to have control of the timing:

s1.png

Basically, the function customCode() takes the array, splices each array inside it into the object of commands and adds it to the list. Even though I can use strings to make the code easier to read, I still need to deal with indents and parameters.

I use it sparingly but helps when I need to write everything in a script call or a function and still want an order of commands to go through. Either that or just use requestAnimationFrame if I am outside the editor or eant something with specific timing haha.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-do-simple-dialogues-show-text-messages-in-plugin-script.178199/