message with choices in a loop

I am trying to create a javascript function in my custom plugin that initiates a conversation in a loop. Text is displayed, and the user is offered the choice to continue or exit. If the user decides to continue, the text is displayed again with the same two options. This repeats until the user selects to exit, at which point the conversation ends.

My final version will of course be more complex, but this example boils down the problem area for me.

I have the logic working fine in RPG Maker MV:

in app logic.png

However, I cannot get this same functionality from my javascript. Here is my attempt:

Code:
Game_System.prototype.endlessConversation = function() {
  $gameMessage.add("Start of dialogue:");
  while (true) {
    $gameMessage.newPage();
    $gameMessage.add("Some text that repeats.");
    $gameMessage.setChoices(["Continue", "Exit"], 0, -1);
    $gameMessage.setChoiceCallback(function(n) {
        if (n === 1) {
          break;
        }
    });
  }     
}

This freezes up because the loop executes infinitely. The loop does not wait for user input between iterations like the one in RPG Maker itself. If I change it to a for loop with a set number of iterations, like 5, it will simply spit out "Some text that repeats." 5 times in a row.

Any ideas on how I can get this javascript code to behave like the RPG Maker logic would be greatly appreciated. Thanks.
The first question I'll ask is, why doesn't the evented solution work for you?
This is really the power of events in that abstracts the inner workings of the code.

For your code, any loop that has "while (true)" is going to repeat indefinitely, because you're not reading any user input.
The key concept to understand is that basically the entire game code runs every frame, which is essentially 60x per second.
Every frame, the code runs to:
  • Gather user input
  • Execute game logic
  • Render sprites
  • Etc.
If you're going to do this, you have to work with the natural game loop, not against it. You should study how the Game_Interpreter works in rpg_objects.js to see how it's done for event commands.
The first question I'll ask is, why doesn't the evented solution work for you?

I will be adding significant complexity to the logic. I removed that complexity here to show just the logic where I am encountering the problem. I'd prefer to manage this complexity in code where I have the full power of javascript available.

For your code, any loop that has "while (true)" is going to repeat indefinitely, because you're not reading any user input.

Right. The evented logic seems to interrupt each loop to let the user respond. The javascript does not, which is what I am attempting to fix.

You should study how the Game_Interpreter works in rpg_objects.js to see how it's done for event commands.

Ok, I suppose I'll have to do that in order to figure out how to mimic the evented solution.
An example which may show better how this works with events currently is the "wait" command.

When there's an active event (parallel on-map etc., parallel common event, action button on-map event, etc.) with a "Wait 60 frames" command, the Javascript code doesn't really wait for 60 frames, it just puts a counter on the event and decrements the counter every frame. If the counter is greater than 0, that event gets ignored for that frame.
Spoiler
Code:
Game_Interpreter.prototype.update = function() {
    while (this.isRunning()) {
        if (this.updateChild() || this.updateWait()) {
            break;
        }
        if (SceneManager.isSceneChanging()) {
            break;
        }
        if (!this.executeCommand()) {
            break;
        }
        if (this.checkFreeze()) {
            break;
        }
    }
};
This is the "update" function that runs every frame for the interpreter. For this example, we're looking at the "updateWait" function.
Spoiler
Code:
Game_Interpreter.prototype.updateWait = function() {
    return this.updateWaitCount() || this.updateWaitMode();
};

Game_Interpreter.prototype.updateWaitCount = function() {
    if (this._waitCount > 0) {
        this._waitCount--;
        return true;
    }
    return false;
};
When the "updateWaitCount" function sees that the frames left to wait are greater than 0, it returns true - which causes the "updateWait" function to return true and causes the "update" function to end. As you read more of the code here, you'll get an understanding of how all the event logic is structured with a frame-by-frame basis.

For your case, I don't think a 'while loop' would be useful. I think a better approach would be recursion, where if the player answers the question the wrong way then the recursive function is just called again. Also, look into `Game_Interpreter.prototype.setWaitMode` to see how the interpreter handles waiting during messages, show choices, etc.

Original thread: https://forums.rpgmakerweb.com/threads/message-with-choices-in-a-loop.103807/