this.wait() not working

I've a simple event.

Wait 10 seconds
Selfswitch to A
Parallel trigger
Code:
this.wait(600);
var greenLight = [002, 002, "A"];
$gameSelfSwitches.setValue(greenLight, true);

But it switches instantly!
Is there a specific reason you have used this.wait instead of the ordinary Event command from the menu?
Is there a specific reason you have used this.wait instead of the ordinary Event command from the menu?
Nope, just learning how to do things in the script window instead of use the Event Command.
The issue is that the wait function applies to the interpreter, which processes event commands: Script is a single event command. After that script call, the interpreter will have a 600 wait count to get through before the next event command is processed.

If you need a script (e.g. variable wait count) then you can get around this by simply putting the wait and switch calls into separate Script commands, one after the other. 🙂
Wait doesn't work on Script Call.
Wait doesn't work on Script Call.
...I think it works for me? 😅
Code:
◆Control Variables:#0001 random = Random 1..5
◆Text:None, Window, Bottom
:    :Let's wait for \v[1] seconds!
◆Control Variables:#0001 random *= 60
◆Script:this.wait($gameVariables.value(1));
◆Text:None, Window, Bottom
:    :It works!
...I think it works for me? 😅
Code:
◆Control Variables:#0001 random = Random 1..5
◆Text:None, Window, Bottom
:    :Let's wait for \v[1] seconds!
◆Control Variables:#0001 random *= 60
◆Script:this.wait($gameVariables.value(1));
◆Text:None, Window, Bottom
:    :It works!

If you try adding other commands, they will ignore the wait call entirely. Because Script call runs everything at once even with wait. It's something I discovered when I tried:
command 1
wait
command 2
wait
If you try adding other commands, they will ignore the wait call entirely. Because Script call runs everything at once even with wait. It's something I discovered when I tried:
command 1
wait
command 2
wait
Oh, sorry, I just misunderstood~ 🙏
The issue is that the wait function applies to the interpreter, which processes event commands: Script is a single event command. After that script call, the interpreter will have a 600 wait count to get through before the next event command is processed.

If you need a script (e.g. variable wait count) then you can get around this by simply putting the wait and switch calls into separate Script commands, one after the other. 🙂
Well, now I need to apply a wait function into a JS plugin. So, what function I've to call to apply a delay into the plugin function? I could use the browser `setTimeout()` or it's discouraged?
Well, now I need to apply a wait function into a JS plugin. So, what function I've to call to apply a delay into the plugin function? I could use the browser `setTimeout()` or it's discouraged?
That, or a Promise (supported in RMMV v1.6+). 🙂

Typically, though, you'd integrate the delay into the existing system, e.g. set a variable that gets reduced by 1 every frame, and invoke a callback if the count is zero that frame. Depends a bit on what you're trying to achieve. 😅
there is no wait function at all - it is called "wait" because that is what it seems to do to a player, but what really happened is something else.
The engine has a main game loop that updates everything once per frame. And what the so-called "wait"-function really does is telling the main loop to stop processing the event or process it is assoziated with for X frames.

That is why a wait in a parallel process event reduces lag - its result is to have the parallel process to be skipped for X frames before it continues.
A true wait-loop would simply freeze the game for that time.

So on the javascript level you need to find those parts of your plugin that are processed, and implement a function into the loop that skips their processing for the time needed.

ninja'd
That, or a Promise (supported in RMMV v1.6+). 🙂

Typically, though, you'd integrate the delay into the existing system, e.g. set a variable that gets reduced by 1 every frame, and invoke a callback if the count is zero that frame. Depends a bit on what you're trying to achieve. 😅
Could you explain more about to reduce a variable every frame?

I've made this for now and it works, but I prefer a thing based on frames.
Code:
function accelerate(eventId) {
 const increaseMoveSpeed = window.setInterval(() => {
  if($gameMap.event(eventId)._moveSpeed <= 4) {
   $gameMap.event(eventId)._moveSpeed += 1;
  } else {
   clearInterval(increaseMoveSpeed);
  }
 }, 200);
}

function decelerate(eventId) {
 const decreaseMoveSpeed = window.setInterval(() => {
  if($gameMap.event(eventId)._moveSpeed >= 3) {
   $gameMap.event(eventId)._moveSpeed -= 1;
  } else {
   clearInterval(decreaseMoveSpeed);
  }
 }, 200);
}
there is no wait function at all - it is called "wait" because that is what it seems to do to a player, but what really happened is something else.
The engine has a main game loop that updates everything once per frame. And what the so-called "wait"-function really does is telling the main loop to stop processing the event or process it is assoziated with for X frames.

That is why a wait in a parallel process event reduces lag - its result is to have the parallel process to be skipped for X frames before it continues.
A true wait-loop would simply freeze the game for that time.

So on the javascript level you need to find those parts of your plugin that are processed, and implement a function into the loop that skips their processing for the time needed.

ninja'd
But I thought that every parallel Event were executed in a different event loop. So it's parallel but in the same event loop.
But I thought that every parallel Event were executed in a different event loop. So it's parallel but in the same event loop
that is the definition of "parallel" - working at the same time, which of course means in the same loop.
and it isn't an event loop - the main game loop contains everything including parallel process events, autonomous movement loops, animations and so on.
Parallel events each have their own interpreter separate from the main one. However, all interpreters are updated via the main game loop that Andar mentions, which originates in the SceneManager. This calls the current scene's update function, which may call its own, specialised, update functions. For example, for events the call chain goes through rpg_scenes.js and rpg_objects.js, something like:
Spoiler
  • Scene_Map
    • update
    • updateMainMultiply
    • updateMain
  • Game_Map
    • update
    • updateEvents
  • Game_Event
    • update
So, if you want something on the map to update every frame, you can alias the event update routine. I haven't tested it, but maybe this example will show vaguely what I mean:
Spoiler
Code:
(function() {

'use strict';

	const ACCEL = 'accelTime';	// Property names...
	const DECEL = 'decelTime';	// ...for ease of maintenance
	const ACCELMAX = 4;		// Accel up to
	const DECELMIN = 3;		// Decel down to
	const TIMER = 6;		// Frame delay between call & speed change
	const ALTER = 0.5;		// Move speed increment/decrement

	// For script calls, init appropriate countdown property for move speed adjustment
	mapEventAccelerate = function(eventId) { $gameMap.event(eventId)[ACCEL] = TIMER; };
	mapEventDecelerate = function(eventId) { $gameMap.event(eventId)[DECEL] = TIMER; };

	// Alias of event update routine, adds speed adjustment after usual update stuff
	var _Game_Event_update = Game_Event.prototype.update;			// local alias
	Game_Event.prototype.update = function() {				// override
		_Game_Event_update.call(this);					// callback
		if (this[ACCEL] !== undefined) {				// is accelerating?
			if (this[ACCEL]-- <= 0) {				// decrement, check for zero
				this._moveSpeed += ALTER;			// increment move speed
				if (this._moveSpeed >= ACCELMAX) {		// has reached target speed?
					this._moveSpeed = ACCELMAX;		// cap at max
					delete this[ACCEL];			// remove property
				} else {
					mapEventAccelerate(this.eventId());	// call accelerate again
				}
			}
		} else if (this[DECEL] !== undefined) {				// is decelerating?
			if (this[DECEL]-- <= 0) {				// decrement, check for zero
				this._moveSpeed -= ALTER;			// etc
				if (this._moveSpeed <= DECELMIN) {
					this._moveSpeed = DECELMIN;
					delete this[DECEL];
				} else {
					mapEventDecelerate(this.eventId());
				}
			}
		}
	};

})();

/// script call usage examples:
 // mapEventAccelerate(5);
 // mapEventDecelerate(31);

Original thread: https://forums.rpgmakerweb.com/threads/this-wait-not-working.108575/