Writing Scenes/Dialog in JS

I think It'd be easier and more robust to handle more of eventing in pure JS and just pass a script function to an event. I tried creating dialog with 2 choices where the first choice would show more dialog. However, I can't seem to get the gameMessage.add to work well..

I call the script this:

1750298725280.png


Here is the code:
JavaScript:
Game_Interpreter.prototype.testShatterEffects = (interpreter) => {
    $gameMessage.add('text');
    $gameMessage.setChoices(['test', 'test2'], 0, 1);
    console.log('this2', this);
    interpreter.setWaitMode('message');
    $gameMessage.add('text3');
    interpreter.setWaitMode('message');

    // $gameMessage.setChoiceBackground(bgType);
    // $gameMessage.setChoicePositionType(posType);
    $gameMessage.setChoiceCallback(function (n) {
        console.log('n', n)
        if (n === 0) {
            console.log('n2', n)
            $gameMessage.add('text2');
            interpreter.setWaitMode('message');
        }
    });
};

I want it to show "text" with the choices, then message "text2" if I click "test" option, and finally then "text3".

What happens is it shows "text" and "text2" in the same line along with the choices. Then it never shows "text2" despite the "n2" console log showing up. I'm doing the messages wrong somehow but I'm not sure. Any ideas?
The first obvious oddity I can see is that you refer to the interpreter as interpreter rather than as this. I don't remember the exact details of how => differs from function() though, so I'm not certain that's wrong.

Next, you need to understand that pretty much everything in RPGMaker is asynchronous. So, $gameMessage.add does not mean "show this message right now". It means "schedule this message to be shown". Similarly, interpreter.setWaitMode('message') does not mean "wait around until the message is dismissed". It means "don't process any more event codes until the message has been dismissed". What that means is that you've made a grave mistake in your flow of logic – you added 'text' and 'text3' essentially at the same time from the game's perspective, so they show at the same time. Your second setWaitMode is redundant and does nothing - it's already set to wait for the message to be dismissed, but that message hasn't even appeared yet.

I'm not completely sure how you can schedule another message to appear after the choice was selected, unfortunately. I'm also not sure why the post-choice message didn't appear. This kinda isn't how the engine expected to be used – it expects to be stepping through the list of codes, one by one.

To be quite honest, I think you're going to find that it is, in fact, neither easier nor more robust to write your events like this. Even I as a seasoned coder would never contemplate doing something like this – the editor exists for a good reason, and I paid good money for it too, and it works pretty well most of the time and isn't all that difficult to use in my opinion. And it does a really good job of smoothing over the asynchronous nature of the engine into something that looks like a synchronous flow of commands, even though it's not.

Still, if you want to keep trying, we can attempt to help you figure things out.
@Solar_Flare explained why this approach doesn't work very well.
For a discussion about an approach that does, check out this thread - it's for MZ, but it will work exactly the same in MV. TLDR: create a new event with JavaScript, and tell the engine to run that event.
@Solar_Flare explained why this approach doesn't work very well.
For a discussion about an approach that does, check out this thread - it's for MZ, but it will work exactly the same in MV. TLDR: create a new event with JavaScript, and tell the engine to run that event.
Thanks! I like the idea of creating a virtual event using javascript. Looks like I can read event json to get the structure I need and I can create functions like showMessage(...details) to push the message to the interpreter.

I'm hoping this will allow me to:
1. write once and reuse the same event everywhere. One update updates all
2. Create faster, more stream lined ways to make events (copy-paste, powerful functions)
2. Mix-match functions to create infinitely unique events/enemies
3. Inheritance and morphing of events
4. Have access to all game functions and call them anywhere
5. Have a deeper control of variables and easier use of variables as list or objects (I've been making a lot of in game variables objects)
6. Can collapse/break down long, convoluted scenes. Giving randomness/complexity to each
7. Can make api calls for "score" data, triviaQuestions, or chat_gpt dialog and instantly use within a scene
7. Keep me fresh with JS for possible jobs and to make it easier to pivot to other game engines

That's my thoughts. Maybe it could be a potential plugin? I haven't dug in too deep atm but the thread shared has me feeling optimistic

Original thread: https://forums.rpgmakerweb.com/threads/writing-scenes-dialog-in-js.178409/