$gameMessage.add()

Hello

I have a problem.
From the Event I call a script, this script read an external text file with the "story" I want to write in the textbox.
I cut the "story" into a maximum of 4 lines each and then I use $gameMessage.add() for write it in the textbox.
Doing so, I write 1-4 lines of text, and then I get the "waiting to continue" part where I need to click/press a key to continue.
When I click/press a key, I get the next part and so on. Everything is ok so far.

Now, each part also have a parameter for the faceimage. both face and index, using $gameMessage.setFaceImage.
But that will always result with the faceimage/index of the last written in the text. The loop will process all the text before writing it in the textbox. So no matter what faceimage/index I have it will just skip it and take the last one.
How can I loop an array of text and write it in the textbox and also have different faceimage/index for each part using a script?

I know I can use the event manager, but then I need to create a new text command for each part.
I really want the story in an external textfile, for easy editing and revision.
I don't know how you're running the code right now, but you need to wait for the message to be shown before adding the new one. For that you can set the Game_Interpreter wait mode to 'message' if you're running those script calls from an event.
Code:
function cut(str, cutStart, cutEnd){
  return str.substr(0,cutStart) + str.substr(cutEnd+1);
}

function ReadExternalTxt(filename){
    var xmlRead = new XMLHttpRequest();
    xmlRead.open("GET","data/" + filename + ".txt",false);
    xmlRead.send(null);
    var msg = xmlRead.responseText.split("|e|");
   
    for (var l = 0; l < msg.length; l++) {
        var x = msg[l].indexOf("|");
        var y = msg[l].indexOf("|", x + 1);
        var c = msg[l].charAt(x + 1);
        switch (c) {
            case "f":
                var i = msg[l].indexOf(",", x + 1);
                var face = msg[l].substring(x + 3, i);
                var index = Number(msg[l].substr(i + 1, 1));
                $gameMessage.setFaceImage(face , index);
            break;      
        }
        msg[l] = cut(msg[l], x, y);
        $gameMessage.newPage();
        $gameMessage.add(msg[l]);
    }
}
this is the code, I call it in the event using a script call: ReadExternalTxt("introduction");
I have tried the setWaitMode but it doesn't work, maybe because I call it from a script?

So I could use the common events instead?
But how do I mix script and show text? I'm inside a loop, that would mean I need to exit the script while in a loop to call the show text? I will try different things, but I appreciate help if you know what to do! 🙂
Yeah, you need to store the text somewhere after loading (a global variable will do), then use a loop command in the event instead of a regular JS loop. at the end of the loop you set the wait mode of the interpreter, so it won't go to the next command until the message is closed.
I manage to make it work using a recursive method.
so, a script, that is called in the event I want to show the text.
this script will store some variables and call the common event.
in the common events:
call another script, that will create different global variables with text and parameters.
then the IF, if the script where successful, if so, show text
and after that I call the common events again (creating a recursive function, like a loop)
using the plugins YEP_MessageCore/Ext I can change the face+index within the show text.

Original thread: https://forums.rpgmakerweb.com/threads/gamemessage-add.83974/