RMMZ - Show choices and messages with script

JavaScript:
$gameMessage.add('Which one to choose?');
$gameMessage.setChoices(['Option 1', 'Option 2'], 0, -1);
var self = this;
$gameMessage.setChoiceCallback(function (n) {
  requestAnimationFrame(function() {
    if (n === 0) {
      Game_Interpreter.prototype.setWaitMode(this._waitMode = 'message')
      $gameMessage.add("You chose option 1");
    }
    if (n === 1) {
      Game_Interpreter.prototype.setWaitMode(this._waitMode = 'message')
      $gameMessage.add("You chose option 2");
    }
  });
});


I'm trying to use script for choices. Above is my code.

But the "show text" command that follows this script always start running before the code above has finished running.
If I put a "show text" after this script, and choose "Option 1", the text shows before "You chose option 1".

I tried adding Game_Interpreter.prototype.setWaitMode(this._waitMode = 'message') but it doesn't work.

What should I do? ;-;
this is a context-dependent keyword. What is the context here? E.g.
  1. A Script command in an event?
    • If so, why not just use the Show Text & Show Choices commands?
  2. In a plugin's notetag or parameter?
    • If so, please state which plugin it is, and give more details about where you're using this.
  3. As part of a plugin's code?
    • If so, please state/show the enclosing scope.
The MV / MZ Script Call List has a valid example for Show Choices.
this is a context-dependent keyword. What is the context here? E.g.
  1. A Script command in an event?
    • If so, why not just use the Show Text & Show Choices commands?
  2. In a plugin's notetag or parameter?
    • If so, please state which plugin it is, and give more details about where you're using this.
  3. As part of a plugin's code?
    • If so, please state/show the enclosing scope.
The MV / MZ Script Call List has a valid example for Show Choices.
Thank you for reply!

1. It's a Script command in an event.

2. Why not just use the Show Text & Show Choices commands?
Because I want to do some calculations in javascript. I think this will save me a lot of work when I set and modify options. (And I want to learn to use scripts.) I'm making a menu like this:

Spoiler: MENU Code
Code:
$gameMessage.add(`MENU`);
const choices = [
{ name: "Food1", price: 5 },
{ name: "Food2", price: 5 },
{ name: "Food3", price: 8 },
{ name: "Food4", price: 8 },
{ name: "Food5", price: 10 },
{ name: "Food6", price: 13 },
{ name: "Food7", price: 13 },
{ name: "Food8", price: 15 },
{ name: "Food9", price: 15 },
{ name: "Food10", price: 20 },
{ name: "Food11", price: 32 },
{ name: "Food12", price: 32 },
{ name: "Food13", price: 32 }
];
$gameMessage.setChoices(choices.map(choice => `${choice.name} ¥${choice.price}`), 0, -1);
$gameMessage.setChoiceCallback(function(n) {
  const selectedChoice = choices[n];
  requestAnimationFrame(function() {
    $gameMessage.add(`You ate ${selectedChoice.name}...\\Wait[15]<br><br>Gold - ${selectedChoice.price}`);
    $gameVariables.setValue(101, $gameVariables.value(101) - selectedChoice.price);
  });
});

After I found the WaitMode problem, I simplified the code to make testing easier:

JavaScript:
$gameMessage.add('Choose?');
$gameMessage.setChoices(['Option1', 'Option2'], 0, -1);
$gameMessage.setChoiceCallback(function (n) {
  requestAnimationFrame(function() {
    if (n === 0) {
    $gameMessage.add("You chose Option1");
    }
    if (n === 1) {
    $gameMessage.add("You chose Option2");
    }
  });
});
this.setWaitMode('message');
$gameMessage.add('This line should appear after the selection');



I read the MV / MZ Script Call List and got to know the setWaitMode thing. So I tried inserting the
this.setWaitMode('message');
in different ways. But it always doesn't work.
Did I do something wrong? /-;
I read the MV / MZ Script Call List and got to know the setWaitMode thing. So I tried inserting the

in different ways. But it always doesn't work.
Did I do something wrong? /-;
Script call/evals process the entire chunk of code all at once. By putting a message below the .setWaitMode(), that message consequently also shows up in the same process along with the previous message.

Use a new script call after each .setWaitMode() function.

Script call 1:
JavaScript:
$gameMessage.add('Choose?');
// Set choices, no cancel
$gameMessage.setChoices(['Option 1', 'Option 2'], 0, -1);
// On choice, set variable #1 equal to the selected index
$gameMessage.setChoiceCallback(n => {
  $gameVariables.setValue(1, n + 1);
});
// Wait for a choice to be selected
this.setWaitMode('message');
Script call 2:
JavaScript:
$gameMessage.add(`You chose Option ${$gameVariables.value(1)}.`);
this.setWaitMode('message');
Script call 3:
JavaScript:
$gameMessage.add('After waiting, this text will appear last.');
this.setWaitMode('message');

Overall, if you can use the event commands, it would be a lot more efficient and the timing is easier to grasp.

Wait commands just delay the specific interpreter and the code that follows it, giving the appearance of "waiting" for the player.

The command executes immediately and goes to the next line of event commands in the event (which is why you can throw a selfswitch early on in the code and if you do not provide a condition to avoid the rest of the code below it, it will still run everything in that event.) However with the event commands, it goes line by line. In a script call chunk, everything inside it executes at once (eval).

In the case of .setWaitMode('message'), what it does is check if $gameMessage is busy, if so, hang out for a bit until it isn't busy anymore. However, make sure you complete all your tasks in the call while you wait.

All wait-type commands in a script call need its own script call because of that (at the very least, make it the last command so you can see your workflow).

Original thread: https://forums.rpgmakerweb.com/threads/rmmz-show-choices-and-messages-with-script.167449/