MZ Best way to script a Wait

I know I've solved this dilemma before in the past and seen quite a few threads that explain it all... maybe it's just because I'm tired but can't really recall what the proper/best way to duplicate the wait command via script...

Little help peeps.. please and thank you 🙂

Looking to code basically this:
Spoiler
1647415820380.png

JavaScript:
$gameMap._interpreter.wait(10);
This doesn't quite do what I would have hoped it would.


Anyways... lol enough rambling...

Thanks in advance for the help with this :popcorn:
(feelin' like a noob even having to ask lol
... and I'm sure I probably do know the answer and will be like oh duh when the answer comes)
I don't have the solution, but perhaps hearing what the wait really is might trigger your memory if you did this before - because the wait is a bit misnomed.

The wait command does NOT tell the engine to wait. Instead it tells "skip processing this event for x frames/cycles of the main game loop".

Basically all events are objects (command lists) in the main game loop, and when the engine skips processing one of them for a number of cycles that looks like a wait to the player for simple action button events.
in the case of parallel processes that is why waits reduce lag - they tell the engine to process the PP less times than every frame.

and you basically need to do the same - not halt the processing, but telling the engine to skip processing your commands at those moments.
Also, in a Script command, you can use this to reference the current interpreter, e.g.
JavaScript:
this.wait(10);
You might need to give more context, i.e.
  1. Where you are trying to use this (a Script command, a plugin, somewhere else?); and
  2. What you are seeing vs what you want to see.
I don't have the solution, but perhaps hearing what the wait really is might trigger your memory if you did this before - because the wait is a bit misnomed.

The wait command does NOT tell the engine to wait. Instead it tells "skip processing this event for x frames/cycles of the main game loop".

Basically all events are objects (command lists) in the main game loop, and when the engine skips processing one of them for a number of cycles that looks like a wait to the player for simple action button events.
in the case of parallel processes that is why waits reduce lag - they tell the engine to process the PP less times than every frame.

and you basically need to do the same - not halt the processing, but telling the engine to skip processing your commands at those moments.
Hmm.. yes I mean I did know this but still helpful & thanks for the quick reply 🙂
Perhaps this could help me figure this out
Also, in a Script command, you can use this to reference the current interpreter, e.g.
JavaScript:
this.wait(10);
You might need to give more context, i.e.
  1. Where you are trying to use this (a Script command, a plugin, somewhere else?); and
  2. What you are seeing vs what you want to see.
Ahhh yes I see.. so I would be using this in a plugin from plugin command triggered from an event or possibly from a common event depending on the user of the plugin.

I just tested this.wait(10) directly from the event itself and it worked exactly as needed. It caused a delay before the next command in the list on the event. So it seems the problem I really have is trying to cause the event to do a "wait" before executing the next command in the list but only because I need to do this from a plugin.

What happens is there is no delay before the next code in the plugin.
What I am trying to achieve is a set amount of frame delay to animate a series of pictures.

Thanks again for all the help figuring this out ☺️

Edit:
Okay sooo... I figured out how to cause the desired amount of wait
That is to use the code to place inside of the Game Interpreter of $gameMap list like so:
JavaScript:
            var i = $gameMap._interpreter._indent;
            var pre = $gameMap._interpreter._list.slice(0, $gameMap._interpreter._index + 1);
            var post = $gameMap._interpreter._list.slice($gameMap._interpreter._index + 1);
            $gameMap._interpreter._list = pre.concat([
             { code: 355, indent: i, parameters: ["console.log('test 1')"]},
             { code: 230, indent: i, parameters: [120] },
             { code: 355, indent: i, parameters: ["console.log('test 2')"]},
            ], post);
This is just a test example..
the above will just print out to console "test 1" then wait 120 frames then print out "test 2"

Now my only problem with that seems to be the player also cannot move around... All other events move.
So I'm wondering how can I do this "wait" to only make the next code "wait" and still allow the player to function as normal? hope the way I asked that makes sense?

I'm guessing this is most likely because this is not being in a parallel process event that calls the plugin command but now I need to figure out how to now also allow the player to move while code in the list is executed.

Thanks again for the replies 🙂

Edit (again):
hmm... yeah there's got to be a better way because now it also seems if I try to execute other code in the game interpreters list while running that from a parallel event it stops the first events code.

Gosh I hope this is making sense to you.
...if not maybe I can make a small demo of what is happening and try to better explain what it is I want to happen.
Yep, that's because you're running it through the main map interpreter, $gameMap._interpreter, regardless of the trigger of the event that called the plugin command. By contrast, each Parallel event runs through its own interpreter, defined on itself. Normal player controls are suspended when the main interpreter is busy, which is why Parallel events run "in the background". So if you want an interpreter to wait X frames in the background, you will need to:
  • Use a Parallel event; 😤
  • Or create and handle your own custom interpreter. 👋
Plugin Commands are run in the context of their interpreter, so this should be an easy fix: just use this in your plugin command function instead of $gameMap._interpreter. Then call the plugin command from a Parallel event. (You will still need to splice the commands in because the wait time only applies to the interpreter.)

Note that your plugin command must be a "normal" function because arrow functions do not have a this value.

(As an aside: I'm not sure why you'd want to dig into the event interpreters with a plugin. In this situation I'd consider either coding the delays into a relevant object's update method, or just using a Common Event. I guess you have a reason, though. 😴)
i remember in srpg we used scenes (scene map) to do that, in cases where scene usage was required..
the good thing is no scene can happen before the other scene is done..

not sure if these screenshots are helpfull..but its clearly a different approach,
also i think all others in this thread have higher JS knowledge than me^^ so i hope this might be understandable..

Screenshot_1.png

---------
this second screenshot is from another srpg plugin as example where it is used:

Screenshot_2.png

in the example above i used it to make the system wait 30 frames before reseting a debug switch
...................
...................

another solution on windows to make moving/steping sprites in windows:

it requires an update function with a counter;
Screenshot_3.png


i guess such update counter solution could be used in a lot of other functions aswell not just for windows

- i mean update functions, update with every frame, and you can use a counter that adds 1 number each frame and if the wanted amount of frames/numbers are archived ,you can call your wanted function or trigger a switch.. (and reset the counter-variable)

..and you can use a script switch with "if condition" to enable/disable the "counter solution" incase its not wanted to repeat it..

--------

All examples i showed are from rpg mv,.. but i guess you can make similar solution on MZ.
And i know my explanations are a bit clumsy here, but i hope that might give you some ideas for other approaches to your problem..
In this situation I'd consider either coding the delays into a relevant object's update method
Yep that's exactly what I ended up doing lol
Things are working now exactly as needed. ☺️

I was definitely just over complicating this.. as I tend to do :blush: 🤣

Thanks again everyone who replied for all the helpful advice. ☺️👍

I think this can be closed now 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/best-way-to-script-a-wait.145813/