$gameMessage.add not showing messages anymore after I do it once

Hello,

I've used rpg maker in the past and I know javascript, but I'm a total begginer in plugins.
So I'm trying to do a simple plugin where the player has to choose between 3 options and depending on the options a text will appear.
The code looks like this :
JavaScript:
/*:
 * @target MZ
 * @plugindesc Custom Card Battle System
 * @command combat
 * @help
 * This plugin implements a custom battle system where the player chooses actions from a set of cards.
 */

(() => {
    const pluginName = "combat";
    class Card {
        constructor(name, action, power) {
            this.name = name;
            this.action = action;
            this.power = power;
        }
    }

    const cards = [
        new Card("Attack", "attack", 10),
        new Card("Heal", "heal", 10),
        new Card("Defend", "defend", 5)
    ];

    function startCardBattle() {
        $gameMessage.add("A wild enemy appears!");
        showCardOptions();
    }

    function showCardOptions() {
        const choices = cards.map(card => card.name);
        $gameMessage.setChoices(choices, 0, -1);
        $gameMessage.setChoiceCallback(onCardChoice);
        $gameMessage.add("Choose a card:");
    }

    function onCardChoice(choiceIndex) {
        const chosenCard = cards[choiceIndex];
        executeCardAction(chosenCard);
    }

    function executeCardAction(card) {
        console.log("card: ", card)
        $gameMessage.add("testtest");
        switch (card.action) {
            case "attack":
                $gameMessage.add(`You attack the enemy for ${card.power} damage!`);
                break;
            case "heal":
                $gameMessage.add(`You heal yourself for ${card.power} HP!`);
                break;
            case "defend":
                $gameMessage.add(`You defend yourself, reducing damage by ${card.power}.`);
                break;
        }
    }

    PluginManager.registerCommand(pluginName, "combat", () => {
        startCardBattle();
    });
})();

Problem is, after I do the choice, and enter the "executeCardAction" function, the $gameMessage.add doesn't show any messages anymore (I tried to do $gameMessage.newPage() but it didn't change anything). And something I don't understand is that I don't get any errors in the console.

Here is the first message:
1716995332965.png


But when I choose an option, the next message doesn't appear, even tho it enters the function because it does the log:
1716995397936.png


Do you have any idea where the issue is ?
Thanks in advance
I think....

Change:
JavaScript:
$gameMessage.add('You attack the enemy for ${card.power} damage!');

Into:
JavaScript:
$gameMessage.add('You attack the enemy for ' + ${card.power} + ' damage!');

Edit: The reason it doesn't work the second time, is because the second time isn't only text, while the first time was. When you add any code, you need to close the message before where you want to show the value, then add the value, then you open the message again to show the rest of the text.

Hope that helps.
It may be because you're not running the commands with an interpreter to wait for the message box to close before attempting to execute the next bits of code.

It really seems like this is the kind of system you would want to power primarily through events with Script commands thrown in as necessary, rather than through a pure plugin.

@JohnDoeNews No, that's correct syntax for a string literal.

1717001976751.png

1717001988095.png
The engine calls onChoice, then clears the message (rmmz_windows.js: Window_ChoiceList#callOkHandler -> Window_Message#terminateMessage). So you can't meaningfully use $gameMessage.add in the choice callback.

It's because Show Choices is designed for use in an event interpreter structure, where any follow-up messages etc will be handled as separate event commands. Like Turan says, I'd suggest redesigning your system to fit that model, e.g. use $gameVariables.setValue(id, value) in the choice callback instead, then use further event commands to check the value of that variable.
It may be because you're not running the commands with an interpreter to wait for the message box to close before attempting to execute the next bits of code.

It really seems like this is the kind of system you would want to power primarily through events with Script commands thrown in as necessary, rather than through a pure plugin.

@JohnDoeNews No, that's correct syntax for a string literal.

View attachment 306749
View attachment 306750
Hi, thanks for your reply.
I thought about that but that wasn't it, if you look in the code, you can see i added a $gameMessage.add("testtest"); but it didn't show either in the game

It may be because you're not running the commands with an interpreter to wait for the message box to close before attempting to execute the next bits of code.

It really seems like this is the kind of system you would want to power primarily through events with Script commands thrown in as necessary, rather than through a pure plugin.

@JohnDoeNews No, that's correct syntax for a string literal.

View attachment 306749
View attachment 306750
Hum i guess i still really need to understand how all of this works, it feels so strange to have something not doing anything without getting error. Thanks for your fast reply, really appreciate it, i figured out how to make it work.

The engine calls onChoice, then clears the message (rmmz_windows.js: Window_ChoiceList#callOkHandler -> Window_Message#terminateMessage). So you can't meaningfully use $gameMessage.add in the choice callback.

It's because Show Choices is designed for use in an event interpreter structure, where any follow-up messages etc will be handled as separate event commands. Like Turan says, I'd suggest redesigning your system to fit that model, e.g. use $gameVariables.setValue(id, value) in the choice callback instead, then use further event commands to check the value of that variable.
Oh ok i see how it works, makes kinda sense now
@JohnDoeNews No, that's correct syntax for a string literal.
Oh, I stand corrected. 😛 I thought I totally solved it.

Original thread: https://forums.rpgmakerweb.com/threads/gamemessage-add-not-showing-messages-anymore-after-i-do-it-once.169093/