Invalid or Unexpected Token Message Box

At some point in my game, an auction takes place. The auctioneer is supposed to loop through a set of predefined dialogue boxes (each loop ending with a choice if the player wants to bid as well) until a randomly number of bids are placed and the item is sold to an NPC. The problem is the dialogue box refuses to work. It's a script instead of the usual UI interface, but this should work regardless. Here's the code:

Code:
$gameMessage.add("$" + ($gameVariables.value(17) * $gameVariables.value(18)) + ",000!");


Values 17 and 18 are the number of bids and price in thousands respectively. When I ran this script (as below) in my browser, it ran fine.

Code:
var b = 1, p = 5;
alert("$" + (b * p) + ",000!");

Fundamentally, they are the same script, so what's going on?
Moved to: Learning JavaScript
(The MV Support board is for questions about base RPG Maker MV behavior. Anything that has to do with code or plugins go somewhere in the JavaScript boards; questions about your own code snippets like this one belong in Learning JS. The members over here have more expertise on this kind of stuff.)

It's unclear what kind of behavior you're getting. You say "The problem is the dialogue box refuses to work." Are you getting a crash or an error? Or does the message box you're expecting simply not appear, and the game continues on happily?

If it's the latter, then I think the reason is because you are adding to $gameMessage which is a string, not an object. So you're telling the engine what the message string should be, but you never call the method necessary to display the message box with that string. And whenever you do call a message box later on (like in an event using the 'Show Message' command), that string has already been overwritten.

I forget what the JS code to show the message box onscreen is - can someone help out?
At some point in my game, an auction takes place. The auctioneer is supposed to loop through a set of predefined dialogue boxes (each loop ending with a choice if the player wants to bid as well) until a randomly number of bids are placed and the item is sold to an NPC. The problem is the dialogue box refuses to work. It's a script instead of the usual UI interface, but this should work regardless. Here's the code:

Code:
$gameMessage.add("$" + ($gameVariables.value(17) * $gameVariables.value(18)) + ",000!");


Values 17 and 18 are the number of bids and price in thousands respectively. When I ran this script (as below) in my browser, it ran fine.

Code:
var b = 1, p = 5;
alert("$" + (b * p) + ",000!");

Fundamentally, they are the same script, so what's going on?
If you're doing this mid-event (e.g. a Script event command), then much like Wavelength says, you need to tell the interpreter to wait for player input, to avoid it getting overridden by subsequent interpreter/message commands. This can be done as follows:
Code:
$gameMessage.add("$" + ($gameVariables.value(17) * $gameVariables.value(18)) + ",000!");
this.setWaitMode('message');
  • EDIT: if using an unusual context where this is not the relevant instance of Game_Interpreter, then you'll need to reference the interpreter object directly, e.g.
    Code:
    $gameMap._interpreter.setWaitMode('message');

Further explanation: Game_Message and alert are very different from one another. You can see this by running your second script call in your RMMV game! The former is an abstracted object handled primarily by the Game_Interpreter instance on the current scene. The latter is a global JavaScript method that tells the browser to spawn a new modal dialog. 😅

You can see that your script call is not equivalent to the Show Text event command (and how you can infer that a setWaitMode call is necessary) by referencing rpg_objects.js:
Spoiler: Relevant code excerpt
Code:
// Show Text
Game_Interpreter.prototype.command101 = function() {
    if (!$gameMessage.isBusy()) {
        $gameMessage.setFaceImage(this._params[0], this._params[1]);
        $gameMessage.setBackground(this._params[2]);
        $gameMessage.setPositionType(this._params[3]);
        while (this.nextEventCode() === 401) {  // Text data
            this._index++;
            $gameMessage.add(this.currentCommand().parameters[0]);
        }
        switch (this.nextEventCode()) {
        case 102:  // Show Choices
            this._index++;
            this.setupChoices(this.currentCommand().parameters);
            break;
        case 103:  // Input Number
            this._index++;
            this.setupNumInput(this.currentCommand().parameters);
            break;
        case 104:  // Select Item
            this._index++;
            this.setupItemChoice(this.currentCommand().parameters);
            break;
        }
        this._index++;
        this.setWaitMode('message');
    }
    return false;
};
😤
Thank you for the replies.

The game crashes upon trying to interpret the script (which is not a plugin but just inside an event). I did manage to get the first message loaded (which would be an NPC shouting the next bid), but the second message still crashes the game. Here is the complete code:

Code:
$gameMessage.add("$"+($gameVariables.value(17)*$gameVariables.value(18))+",000!");
this.setWaitMode("message");

// Second script (separate block)

$gameMessage.setFaceImage("Auctioneer", 0);
$gameMessage.add("$"+($gameVariables.value(17)*$gameVariables.value(18))+",000! Do I hear $"+(($gameVariables.value(17)+1)*$gameVariables.value(18))+",000?);
this.setWaitMode("message");

Again the first block of code works as intended. But the second block fails due to a syntax error, according to the game, and stops any further execution.

The loop, by the way, runs as follows:

Code:
// Psuedo-code
loop
    Ask for player input (if they want to bid or not)
    Display audience bid (if player did not bid)
    Display auctioneer's response
    if bids == 17 (example number)
        break loop
Looks like you forgot the closing quotes on the message string, maybe try this instead:
Code:
$gameMessage.add("$"+($gameVariables.value(17)*$gameVariables.value(18))+",000! Do I hear $"+(($gameVariables.value(17)+1)*$gameVariables.value(18))+",000?");
Code editor apps (e.g. Visual Studio Code, Atom, etc) often feature syntax highlighting, which can be handy for spotting mistakes like that. 🙂

Just to note, the pseudocode looks doable with only event commands. In case you're unaware: you can display a variable value in Show Text using \v[x], replacing x with the ID of the variable, e.g. for your first script call:
Code:
◆Control Variables:#0019 bidAmount = var17
◆Control Variables:#0019 bidAmount *= var18
◆Text:None, Window, Bottom
  :    :$\v[19],000!
Well, don't I feel stupid. I use Notepad++ (since I don't need an entire IDE for JavaScript), so I should have seen this error; I was just focused on finding an error regarding parenthesis since those trip me up the most.

Thank you!
It's an easy mistake to make. 🙂 Happy RPG Making! 😂

Original thread: https://forums.rpgmakerweb.com/threads/invalid-or-unexpected-token-message-box.112649/