How to Wait for Script Execution Complete inside an Event

Hi there,
I just found that if I arrange a 'Message' after a 'Script' inside an event, the message will render before the script has executed completely. Despite of message, other type of operations also won't wait for script execution.

I would like to know if there are any standard approaches to let event wait for the execution of script to complete before moving on the the next operation.

Thanks in advance.
Hi, although I have a suspicion as to what is going on, could you please share a screenshot of the event in question? The whole screen, preferably.

I would like to know if there are any standard approaches to let event wait for the execution of script to complete before moving on the the next operation.
I’ve come to appreciate that nothing is standard when it comes to “wait”. The engine needs to go through a lot of loops and holes to facilitate waiting and synchronizing.
We usually use "Script" for short JS code snippets only and not for complicated logic. Because the internal interpreter does not wait when going through the Events besides a few corner cases, such as "Move Route," "Show Balloon," "Show Animation," etc. Unfortunately, I don't know a magic line to change this behavior.

I suggest using Switches. At the end of your script, add something like this:
Code:
$gameSwitches.setValue(99, true)
Replace 99 with any unused Switch Id.

In your Eventing, you can do something like this:
Code:
Loop
  IF Switch 99 is ON
    BREAK Loop
  END IF
End Loop
Set Switch 99 to OFF
There may exist many other and even better ways than this one.

I also think you need to share some more details about what you're trying to achieve.
Hi, although I have a suspicion as to what is going on, could you please share a screenshot of the event in question? The whole screen, preferably.


I’ve come to appreciate that nothing is standard when it comes to “wait”. The engine needs to go through a lot of loops and holes to facilitate waiting and synchronizing.
Sure, here it is. I have simplified the situation so we can focus on this topic. What I wish to achieve is the Message could print '100'. But like this post implies, it's not waiting for the script and print '0'
1692116411682.png
We usually use "Script" for short JS code snippets only and not for complicated logic. Because the internal interpreter does not wait when going through the Events besides a few corner cases, such as "Move Route," "Show Balloon," "Show Animation," etc. Unfortunately, I don't know a magic line to change this behavior.

I suggest using Switches. At the end of your script, add something like this:
Code:
$gameSwitches.setValue(99, true)
Replace 99 with any unused Switch Id.

In your Eventing, you can do something like this:
Code:
Loop
  IF Switch 99 is ON
    BREAK Loop
  END IF
End Loop
Set Switch 99 to OFF
There may exist many other and even better ways than this one.

I also think you need to share some more details about what you're trying to achieve.
I believe this logic could solve my problem. But I can't admit that it's a very elegant way to solve this problem. Still thank you for providing this idea. I will take this measure if nothing better comes in this post.
Code:
Loop
  IF Switch 99 is ON
    BREAK Loop
  END IF
End Loop
Set Switch 99 to OFF
You would want a Wait command in there, otherwise it'll repeat the check an unlimited number of times per frame. 😅

I have simplified the situation so we can focus on this topic.
This thinking is rarely helpful. For example, why would you not just do that with event commands?

◆Wait:60 frames ◆Control Variables:#0001 = 100 ◆Text:None, None, Window, Bottom : :\v[1]
If that doesn't solve your problem, please give a more representative example of what you're trying to do.

What I wish to achieve is the Message could print '100'. But like this post implies, it's not waiting for the script
RPG Maker's event interpreters have built-in wait mechanisms. You can manually tell it to wait X frames using the Wait command. Alternatively there may be an appropriate wait mode, if you want to wait for something to happen first, but you don't know when it is going to happen. If there isn't, it may be possible to find a plugin that implements a new wait mode for your use case. Or just event a wait mode using Loop, Conditional Branch, Wait, and Break Loop.

Note that setTimeout is a bad idea in this case because you cannot reliably predict when during a frame update, if at all, that it will actually execute. It can also potentially carry over when the engine expects things to reset, e.g. quitting to the title or loading a save won't cancel any existing timeouts in progress. 👋
Sure, here it is. I have simplified the situation so we can focus on this topic. What I wish to achieve is the Message could print '100'. But like this post implies, it's not waiting for the script and print '0'
Not exactly sure why you thought this is adequate as an isolation of the problem. I mean, why not just share the actual problem?

In this particular case and on my machine, adding "this.wait(62)" at the bottom of the script causes the text box to somewhat consistently display "100" whereas "this.wait(61) will cause the text box to sometimes display "0" and sometimes display "100".

◆Script:$gameVariables.setValue(40,0); : :setTimeout(() => { : :$gameVariables.setValue(40,100); : :},1000); : :this.wait(61); ◆Text:None, None, Window, Bottom : :\v[40]

So I'm not sure if you should be approaching this with "wait". If your example is truly representative of the actual situation, I would just replace by a simple ◆Control Variables:#0001 = 100(1,100)... without any scripting.
Note that setTimeout is a bad idea in this case because you cannot reliably predict when during a frame update, if at all, that it will actually execute.
This is a good example of that unpredictability:
In this particular case and on my machine, adding "this.wait(62)" at the bottom of the script causes the text box to somewhat consistently display "100" whereas "this.wait(61) will cause the text box to sometimes display "0" and sometimes display "100".
Despite RMMV's fluid timestep implementation, the time taken for the game to execute 1 frame is still variable and dependent on the end user's system. It doesn't have to be due to poor performance, either: the game may finish updating faster on the intended frame than the one when you started the timer, causing the change to be seen 1 frame late.

If you want some kind of background timer or "fake async", use a Parallel event. Note that 1 Parallel event can manage multiple timers, e.g. have 1 variable per timer, and have the Parallel event count down each active timer by 1 every tick, turning on an appropriate switch or calling an event or something whenever any variable hits 0.
I have simplified the situation so we can focus on this topic.
No, you haven't simplified anything, but made that screenshot completely useless.

Because it does not contain the important part, the commands about the mnessage you want to give:
I arrange a 'Message' after a 'Script' inside an event, the message will render before the script has executed completely.

The makers have one single main game loop that executes the objects it contains one after another.
For example every parallel process event starts its own game_interpreter object inside that game loop.

Now if you have two things that happen independently of each other instead of one after another, that means that you created an additional object inside the game loop.
and to solve that, we need to see the commands you used to create that additional object instead of using the current object to process the commands (which would keep everything in sequence).

The event commands have their API written specifically to handle tha game loop in the intended sequences - but if you use scripts, then that security is gone and you are responsible for handling the timing yourself.
For that reason alone you should use the event commands whenever possible and only switch to scripts if absolutely neccessary and you're knowing what you're doing.
Script functions are NOT always the better solution and you should not switch to scripts unless you have to.

And now please show us the correct script you have, without removing anything from it. We can't give you a solution otherwise.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-wait-for-script-execution-complete-inside-an-event.160314/