MZ Help with waiting for Message window to close

Hi,
I'm looking for guidance on how I can control `gameMessage` to show up only after the previous one is closed inside a plugin command.

The code I tried (after some forum investigation) looks like this:
JavaScript:
(() => {
    PluginManager.registerCommand('MyPlugin', 'start', args => {
       
        $gameMessage.setPositionType(1);
        $gameMessage.add("Welcome to MyPlugin");
     
        $gameMap._interpreter.setWaitMode('message');
       
        //Second message in a new window (text box) after the user closes (clicks on the first)
        $gameMessage.add('This is a new message');
    })
})();

but both messages are shown in the same window on separate lines.
The behavior I want to replicate looks like this:
1720630942424.png
but both messages are shown in the same window on separate lines.
that is because you created a single message with two lines, without giving the engine time to display the first part.

you create a message, set a wait mode, then add to the created message and then the function end, and THEN the engine has time to display the message you created with both lines.

Unfortunately you'll have to wait for someone else to give you the exact code for what you want as I never used MZ, but you should start looking up the main game loop if you want to program at that level.
Hi,
I'm looking for guidance on how I can control `gameMessage` to show up only after the previous one is closed inside a plugin command.

The code I tried (after some forum investigation) looks like this:
JavaScript:
(() => {
    PluginManager.registerCommand('MyPlugin', 'start', args => {
      
        $gameMessage.setPositionType(1);
        $gameMessage.add("Welcome to MyPlugin");
    
        $gameMap._interpreter.setWaitMode('message');
      
        //Second message in a new window (text box) after the user closes (clicks on the first)
        $gameMessage.add('This is a new message');
    })
})();

but both messages are shown in the same window on separate lines.
The behavior I want to replicate looks like this: View attachment 310901
I literally just add a 1 frame wait time to break issues like this up.
If the messages or their number are predictable, it's probably easier to instead run a common event or split the plugin command into multiple ones.

Otherwise you have two options:
- Modify map scene's update method, and check if your message is finished before displaying the next one
- Create a list of commands and run them with this.setupChild. This requires more knowledge, but is much nicer.

How experienced are you with JavaScript?
that is because you created a single message with two lines, without giving the engine time to display the first part.

you create a message, set a wait mode, then add to the created message and then the function end, and THEN the engine has time to display the message you created with both lines.

Unfortunately you'll have to wait for someone else to give you the exact code for what you want as I never used MZ, but you should start looking up the main game loop if you want to program at that level.
Thanks for pointing out the main game loop - will try looking it up
I literally just add a 1 frame wait time to break issues like this up.
Could you share a code snipped because it seems to me that it would wait one frame to show the new window rather than wait for the user to close the first one
If the messages or their number are predictable, it's probably easier to instead run a common event or split the plugin command into multiple ones.
This is just a POC of a minigame I want to make. While a common event seems reasonable I do not want to trash control variables with a bunch of minigame state variables.
- Modify map scene's update method, and check if your message is finished before displaying the next one
The issue I see with this approach is that I would like to have multiple different "type" of wait for's - message closed, user inputs a number, clicks on an image, etc.
How experienced are you with JavaScript?
Intermediate - FE web developer
Intermediate - FE web developer
Alright, then this should be reasonably simple for you.

What you can do is create a "virtual" event (like the one in the screenshot you shared), in the form of a list of commands that you need. Then simply use the setupChild method of the interpreter and give it that list.

To figure out how I recommend creating a new map, and within it a single event, with a single page, containing more or less the commands you'll need. Then open the json file for that map, find that one event, and specifically the "list" property: this should give you a good idea of how should such a list look like.

A simple example for showing two messages would be:
JavaScript:
let commandList = [];
commandList.push({ code: 101, indent: 0, parameters: ["", 0, 0, 2] });
commandList.push({ code: 401, indent: 0, parameters: ["Hello there"] });

commandList.push({ code: 101, indent: 0, parameters: ["", 0, 0, 2] });
commandList.push({ code: 401, indent: 0, parameters: ["This is a second message"] });
commandList.push({ code: 401, indent: 0, parameters: ["And that's its second line"] });
this.setupChild(commandList, 0)

EDIT: And I've just realised that this is MZ and not MV, so the above code may or may not work. But the overall approach described in this post still should.
Hi there!
You really want that to be via code? Because it seems to me that you can do that by just eventing.
Could you share a code snipped because it seems to me that it would wait one frame to show the new window rather than wait for the user to close the first one
I think what @Andreavnn means is to put the event command 1 wait frame between the swho text commands.
The window, by default, takes up an 8 frames to open or close. If you put a wait like 10 or 15 frames you can already get a nice effect:
1720643105759.png



Desktop10-07-202417-26-07-ezgif.com-video-to-gif-converter.gif
You really want that to be via code? Because it seems to me that you can do that by just eventing.
As mentioned above the code snippet is just a PoC for me to figure out how to sequence Event commands - Show Text is just a simple example to not distract from the real question.
I think what @Andreavnn means is to put the event command 1 wait frame between the swho text commands.
I still don't understand the proposed approach. Something like this does not work at all (does not wait for close or for the time to elapse):

JavaScript:
    PluginManager.registerCommand('MyPlugin', 'second', args => {
        $gameMessage.setPositionType(1);
        $gameMessage.add("Welcome to MyPlugin");

        $gameMap._interpreter.wait(1200);

        $gameMessage.setPositionType(1);
        $gameMessage.add("This is the second message.");
    });
Kudos to Mac15001900 for providing a working solution. I'll expand on it and give some more details for anyone in the future. You can also reference https://forums.rpgmakerweb.com/inde...terpreter-and-you-what-makes-an-event.152035/ for further details.

As Mac mentioned, easiest setup is to create a mock event in an empty map.
1720708476448.png


Then you go to the maps JSON file that can be found in /data folder and find the events "list" property. It will look something like this:
1720708647953.png


Afterwards you can construct you command list based on the mock event:
JavaScript:
 PluginManager.registerCommand('MyPlugin', 'start', args => {
        let commandList = [];

        // Show first textbox
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["Welcome to MyPlugin"]});
    
        // Show second textbox
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["This is the second message."]},{"code":0,"indent":0,"parameters":[]});

        // Play SE
        commandList.push({"code":250,"indent":0,"parameters":[{"name":"Attack2","volume":90,"pitch":100,"pan":0}]});

        // Show Picture; Wait 120 frames; Erase Picture
        commandList.push({"code":231,"indent":0,"parameters":[1,"Actor1_1",0,0,0,0,100,100,255,0]});
        commandList.push({"code":230,"indent":0,"parameters":[120]});
        commandList.push({"code":235,"indent":0,"parameters":[1]});

        //Ask question
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["Did you enjoy the demo?"]});

        //Show choice
        commandList.push({"code":102,"indent":0,"parameters":[["Yes","No"],1,0,2,0]});

        commandList.push({"code":402,"indent":0,"parameters":[0,"Yes"]});
        //Notice "indent" - nested choice logic
        commandList.push({"code":101,"indent":1,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":1,"parameters":["Cool!!!"]},{"code":0,"indent":1,"parameters":[]});
        //Exit nested logic
        commandList.push({"code":0,"indent":1,"parameters":[]});

        commandList.push({"code":402,"indent":0,"parameters":[1,"No"]});
        commandList.push({"code":101,"indent":1,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":1,"parameters":["Too bad :("]});
        commandList.push({"code":0,"indent":1,"parameters":[]});

        //Auto generated cleanup commands
        commandList.push({"code":404,"indent":0,"parameters":[]});
        commandList.push({"code":0,"indent":0,"parameters":[]});
        
    
        $gameMap._interpreter.setupChild(commandList);
    });

Conclusion - while this approach is feasible, it quickly becomes verbose and prone to typos/errors. Will reevaluate the need for waiting between commands and general approach of the task.
to figure out how to sequence Event commands - Show Text is just a simple example to not distract from the real question.
in that case you need to keep in mind that not all event commands are equal - there is a difference between direct commands and queued commands as well as how the queue is handled.

Because with parallel processes, a lot of events can run at the same time, which also means that many instances of the interpreter are active. To help keeping that in line, several commands do not execute their effects directly but hand them to a queue that is processed independenly of the event giving those commands.

This is most obvious in the set move route command, because that command does not move any event - as the name suggests it adds the prepared move route as an additional command list into the main game loop, and has the event giving the command continue independently. Only the wait for completion flag of set move route tells the current event to wait until that other command list is completed, otherwise both are runnning parallel.

It is less obvious in show text and call common event, but both of those do basically the same. Only in the event framework this is hidden by automatically synchronising the wait, but as both script command names show (add message instead of show message etc) the script commands are outside the event framework and need additional work to have the interpreters wait, otherwise the text or the common event are only added to their queues to be processed when the engine decides to process them and not where you place the command to place them into the queue.

Only direct commands like adding to inventory and so on are indeed processed when you give the command.


and now to your mistake with the wait command.
Because neither the scripted wait command nor the event wait frames do what you think they do.
The wait function is part of the main game loop that runs once per frame. And what it tells the game loop is "Skip my event/interpreter for this cycle of the game loop"

As such, the wait command alone does nothing. It needs to be attached to an object in the game loop to do anything.
The event framework takes care of this by adding it to the event interpreter of the event that it is used in.
But if you use it as script command, you need to take care that it is attached to the correct instance of the interpreter that contains the command you want to wait before processing.
And as you have seen in your try above, telling a random map interpreter to wait is not the same as telling your own script functions to wait, because those are outside any event game interpreter.

No, I can't tell you how to identify which instance of which game loop object you need to attach the wait to for your code to cause it to wait - I never went that deep into the code and scripting myself.
Kudos to Mac15001900 for providing a working solution. I'll expand on it and give some more details for anyone in the future. You can also reference https://forums.rpgmakerweb.com/inde...terpreter-and-you-what-makes-an-event.152035/ for further details.

As Mac mentioned, easiest setup is to create a mock event in an empty map.
View attachment 311015

Then you go to the maps JSON file that can be found in /data folder and find the events "list" property. It will look something like this:
View attachment 311016

Afterwards you can construct you command list based on the mock event:
JavaScript:
 PluginManager.registerCommand('MyPlugin', 'start', args => {
        let commandList = [];

        // Show first textbox
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["Welcome to MyPlugin"]});
   
        // Show second textbox
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["This is the second message."]},{"code":0,"indent":0,"parameters":[]});

        // Play SE
        commandList.push({"code":250,"indent":0,"parameters":[{"name":"Attack2","volume":90,"pitch":100,"pan":0}]});

        // Show Picture; Wait 120 frames; Erase Picture
        commandList.push({"code":231,"indent":0,"parameters":[1,"Actor1_1",0,0,0,0,100,100,255,0]});
        commandList.push({"code":230,"indent":0,"parameters":[120]});
        commandList.push({"code":235,"indent":0,"parameters":[1]});

        //Ask question
        commandList.push({"code":101,"indent":0,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":0,"parameters":["Did you enjoy the demo?"]});

        //Show choice
        commandList.push({"code":102,"indent":0,"parameters":[["Yes","No"],1,0,2,0]});

        commandList.push({"code":402,"indent":0,"parameters":[0,"Yes"]});
        //Notice "indent" - nested choice logic
        commandList.push({"code":101,"indent":1,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":1,"parameters":["Cool!!!"]},{"code":0,"indent":1,"parameters":[]});
        //Exit nested logic
        commandList.push({"code":0,"indent":1,"parameters":[]});

        commandList.push({"code":402,"indent":0,"parameters":[1,"No"]});
        commandList.push({"code":101,"indent":1,"parameters":["",0,0,2,""]});
        commandList.push({"code":401,"indent":1,"parameters":["Too bad :("]});
        commandList.push({"code":0,"indent":1,"parameters":[]});

        //Auto generated cleanup commands
        commandList.push({"code":404,"indent":0,"parameters":[]});
        commandList.push({"code":0,"indent":0,"parameters":[]});
       
   
        $gameMap._interpreter.setupChild(commandList);
    });

Conclusion - while this approach is feasible, it quickly becomes verbose and prone to typos/errors. Will reevaluate the need for waiting between commands and general approach of the task.
I'm glad you got it to work. Some further tips:
- When using both commands and JavaScript, it's often useful to have the last command be a script call that simply calls the next part of your script.
- If you plan on doing this a lot, make some wrapper functions around those commands, so you don't need to type them each time. For example, here's a function from my game that takes a list of strings and displays them as messages (with a few additional bells and whistles):
Spoiler: showMessages
1720726002078.png
I'm glad you got it to work. Some further tips:
- When using both commands and JavaScript, it's often useful to have the last command be a script call that simply calls the next part of your script.
- If you plan on doing this a lot, make some wrapper functions around those commands, so you don't need to type them each time. For example, here's a function from my game that takes a list of strings and displays them as messages (with a few additional bells and whistles):
Spoiler: showMessages
Yeah, was thinking the same way. If I was serious with this approach I would probably use TS and use enums and decorators - but that's a different topic.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-waiting-for-message-window-to-close.170179/