MV Plugin: Wait before executing a specific call.

So I have a plugin that listens to a specific custom keystroke, once the key is triggered it shows some PIXI text on the screen.
For the PIXI text to be shown it needs to be added as a child to the SceneManager.
Now as long as the PIXI text is a child of the SceneManager it will be shown on the screen.
I would like for the text to go away after a while. I can do this with either of two ways (it doesn't matter to me which of the two).
  1. Remove the child from the SceneManager after a couple of seconds.
  2. Change the Text content to be empty (probably more efficient since I can re-write to this text again?) after a couple of seconds.
Now my problem as you might imagine is that the way I've written my code both the call that sets the Text as a child of the SceneManager as well as the code that removes it are evaluated at the same time. As far as I can understand (not that much) I should not use the setTimeout function because the Game loop will poop itself...😛 (although I did and it did not work).
I tried to use the wait function of the GameInterpreter but it had no effect.

So to cut a long story short:
Is there a Best Practice way to wait for a couple of seconds before a specific function is called in my plugin?
if you use it as a scriptcall, why not use a autorun effect?
wait x frames, SS ON and done.

setTimeout should work, as well for "this.wait(frames)".

however, it might not work when it is running in parallel, but does
work on action trigger and the likes.

so the best way if not an action trigger, use autorun for this effect.
I had everything in a parallel common event however I wanted to turn it to a plugin so no this.wait() doesn't work in there.
A lot to unpack here...
  • Depending on what your display object is (a sprite? I don't really know what "PIXI text" refers to, and you didn't provide code)... you may want to clarify or adjust how you introduce it. Maybe by adding it as a child to the scene's spriteset or perhaps its window layer as opposed to adding it as a direct child of the scene itself.
  • If you are only displaying one instance of text at a time, then yes, you can re-use it by clearing its contents as opposed to creating and destroying the display object each time you want to display a new instance of text.
  • One of the primary reasons you would not use setTimeout is... not the reason you cited. It's because MV and MZ update on a frame-by-frame basis rather than intervals like seconds.
  • Most windows, scenes, sprites, etc. have an update method that fires every single frame.
  • You would use a wait method called in the update method that determines whether or not to wait before doing any further processing. You generally can't arbitrarily call GameInterpreter's wait method on random things. You would instead need to leverage a wait method provided by the scene, window, or sprite.
  • There are numerous existing wait methods you can use as examples in the default code. You can look at the update methods for Window_BattleLog, Window_Message, and others by browsing through the core script files.
Parallel keeps the game running, so "wait" wont work in parallel as you can
keep moving around.

wait in action trigger and autorun stops player movement until the "wait" is over.
A lot to unpack here...
  • Depending on what your display object is (a sprite? I don't really know what "PIXI text" refers to, and you didn't provide code)... you may want to clarify or adjust how you introduce it. Maybe by adding it as a child to the scene's spriteset or perhaps its window layer as opposed to adding it as a direct child of the scene itself.
  • If you are only displaying one instance of text at a time, then yes, you can re-use it by clearing its contents as opposed to creating and destroying the display object each time you want to display a new instance of text.
  • One of the primary reasons you would not use setTimeout is... not the reason you cited. It's because MV and MZ update on a frame-by-frame basis rather than intervals like seconds.
  • Most windows, scenes, sprites, etc. have an update method that fires every single frame.
  • You would use a wait method called in the update method that determines whether or not to wait before doing any further processing. You generally can't arbitrarily call GameInterpreter's wait method on random things. You would instead need to leverage a wait method provided by the scene, window, or sprite.
  • There are numerous existing wait methods you can use as examples in the default code. You can look at the update methods for Window_BattleLog, Window_Message, and others by browsing through the core script files.
Thanks a lot and indeed there is a lot to unpack.
To make it more clear I am developing a system where the player can press certain buttons and interact with events that have an appropriate note-tag.
For example there is an "Inspect" button that when pressed finds the closest event to the player that has the tag <inspect:true> and then displays some text above it.
Another example is when a player presses a "Push" button and a direction key in front of a "pushable" event then the event moves one tile in that direction.
Now I have to enforce a wait between button pushes because I don't want the player to spam "Pushing".
e.g.

JavaScript:
function Push() {
    filter_events = getPushableEventInFrontOfPlayer()
    if (Input.isTriggered('down') && $gamePlayer._direction===2) {
          let event = filtered_events.find(event=>event._y-1===$gamePlayer.y)
          if (event) {
            let moveRoute = {"list":[{"code":32,"parameters":[]},{"code":29, "parameters":[5]}, {"code":30, "parameters":[5]}, {"code":11,"parameters":[]}, {"code":31,"parameters":[]}],"repeat":false,"skippable":false,"wait":false}
            if($gamePlayer.x===event._x && $gamePlayer.y+1===event._y){event.forceMoveRoute(moveRoute);event.requestBalloon(1);}
            }
    }
}
  Scene_Map.prototype.updateInteractionEvents = function() {
    if (Input.isPressed('s')) {
      Push();
    }
  };
The way that the code is as long as I keep 's' press and spam the 'down' key it tries to push but I want to introduce a wait time. What is the best way to do this?
Thanks a lot and indeed there is a lot to unpack.
To make it more clear I am developing a system where the player can press certain buttons and interact with events that have an appropriate note-tag.
For example there is an "Inspect" button that when pressed finds the closest event to the player that has the tag <inspect:true> and then displays some text above it.
Another example is when a player presses a "Push" button and a direction key in front of a "pushable" event then the event moves one tile in that direction.
Now I have to enforce a wait between button pushes because I don't want the player to spam "Pushing".
e.g.

JavaScript:
function Push() {
    filter_events = getPushableEventInFrontOfPlayer()
    if (Input.isTriggered('down') && $gamePlayer._direction===2) {
          let event = filtered_events.find(event=>event._y-1===$gamePlayer.y)
          if (event) {
            let moveRoute = {"list":[{"code":32,"parameters":[]},{"code":29, "parameters":[5]}, {"code":30, "parameters":[5]}, {"code":11,"parameters":[]}, {"code":31,"parameters":[]}],"repeat":false,"skippable":false,"wait":false}
            if($gamePlayer.x===event._x && $gamePlayer.y+1===event._y){event.forceMoveRoute(moveRoute);event.requestBalloon(1);}
            }
    }
}
  Scene_Map.prototype.updateInteractionEvents = function() {
    if (Input.isPressed('s')) {
      Push();
    }
  };
The way that the code is as long as I keep 's' press and spam the 'down' key it tries to push but I want to introduce a wait time. What is the best way to do this?
The best way to handle this wouldn't be using delays at all. Logically, what you want to do it is to only add the move route when there isn't one already present. So just check for that, e.g. in that last if.

JavaScript:
 if($gamePlayer.x===event._x && $gamePlayer.y+1===event._y && !event._moveRouteForcing){event.forceMoveRoute(moveRoute);event.requestBalloon(1);}

Doing this with delays would break it if the event has a particularly slow movement speed, which seems like something you might want to add at some point for a very heavy object.

For a more general answer, usually the way to add a delay is to save the time you start it (Graphics.frameCount is quite useful here), and in an update function check if the amount of frames that passed since then is enough. Though if you can avoid that (e.g. by directly checking the state you want like above) it's generally better to do so.

Original thread: https://forums.rpgmakerweb.com/threads/plugin-wait-before-executing-a-specific-call.162061/