Making an event wait a number of frames equal to a randomized variable.

Okay, so, a game project I am working on is almost ready for its next release. The only feature not currently implemented to the new content is a racing minigame that the player can participate in for cool rewards, including some key items for later end-game stuff I have planned. I already have the bulk of the basic mechanics figured out, and they seem to work as intended. However, there is one thing I have yet to nail down.

For context, I plan for there to be 5 racers aside from the player, and a grand total of 5 difficulty settings. Very easy to Very Hard. You know, the usual spread. The idea is that NPC racers will be more likely to move faster on higher difficulties.

The way I want to handle this is by having a variable be randomized periodically, and the racer associated with that variable must wait a number of frames equal to that randomized variable between each step. I may even apply this limitation to the player using the same variable range as on normal difficulty - assuming I can find a way to make that feel good to play. the variable range will be larger and start at a higher number on lower difficulties. My hope here is to simulate different speeds and the fluctuating pace of an intense, competitive race without just flipping around the event speed setting. The differences in speed that creates are too drastic to feel natural or particularly compelling.

For clarity, each racer has its own variable for modifying its speed.

I looked it up before, and I was directed to use a script call, but it does not seem to work as I want. Namely, in that it doesn't work at all.

I have attached a couple of screenshots showing how my events are currently formatted. They are only active when a race is in progress, and all are parallel processes.

P.S: While not related to this particular issue, I am also having an issue where the racers just stop if the player gets too far away. If anyone knows of a way around that off-hand, maybe point me in the right direction?

TL;DR - Need Events to wait before taking their next step based on a random variable that's changing a bunch. Not presently working. Help, please.
It sounds like a move route wait would be good in your situation. Maybe try something like this in a move route Script [edit: corrected from this.wait(etc) to this._waitCount = etc]:

this._waitCount = Math.randomInt(15) + 1;
I.e. "get a random number from 0~14 and add 1, then make this character wait that many frames". Or, more simply "wait 1~15 frames before the next move command".

This may also allow you to remove the Parallel events, since the random number will be re-rolled each time the move route processes that script call.

Some explanation​

There's a difference between event commands and move route commands:
  • Event: wait applies before doing the next event command.
  • Move route: wait applies before doing the next move command.
As for the approaches in your screenshots, both of them are for making the event wait:
  1. Without scripting, you'd need to roll the random wait time once (per wait), then loop until that wait time is complete, e.g.
    Spoiler: Event example (random wait time)
    ◆Comment:Get random wait time ◆Control Variables:#0020 Time = Random 1..15 ◆Comment:Loop until wait time is complete~ ◆Loop ◆If:Time > 0 ◆Comment:Tick down counter ◆Control Variables:#0020 Time -= 1 ◆Comment:Check again later ◆Wait:1 frame ◆ :Else ◆Comment:Time's up! ◆Break Loop ◆ :End ◆ :Repeat Above ◆Text:None, Window, Bottom : :Done!
    In your screenshot you have a loop that never ends, since there is no Break Loop command.

  2. $gameMap._interpreter.wait($gameVariables.value(27)) makes the current non-Parallel event wait (if there is one running). Parallel events each run through their own interpreter; there's no convenient way to reference "this" event/interpreter from within a move route.
This link should help with the offscreen movement, but I think it could probably cause some lag if overused.

Otherwise, it looks like caethyril has you covered on the rest. Good luck!
Thanks for the info you two. I'll give it a whirl later in the day when I'm not engrossed in another matter and get back to you on whether or not it works.
It sounds like a move route wait would be good in your situation. Maybe try something like this in a move route Script:

this.wait(Math.randomInt(15) + 1);

I.e. "get a random number from 0~14 and add 1, then make this character wait that many frames". Or, more simply "wait 1~15 frames before the next move command".

This may also allow you to remove the Parallel events, since the random number will be re-rolled each time the move route processes that script call.

Some explanation​

There's a difference between event commands and move route commands:
  • Event: wait applies before doing the next event command.
  • Move route: wait applies before doing the next move command.
As for the approaches in your screenshots, both of them are for making the event wait:
  1. Without scripting, you'd need to roll the random wait time once (per wait), then loop until that wait time is complete, e.g.
    Spoiler: Event example (random wait time)
    ◆Comment:Get random wait time ◆Control Variables:#0020 Time = Random 1..15 ◆Comment:Loop until wait time is complete~ ◆Loop ◆If:Time > 0 ◆Comment:Tick down counter ◆Control Variables:#0020 Time -= 1 ◆Comment:Check again later ◆Wait:1 frame ◆ :Else ◆Comment:Time's up! ◆Break Loop ◆ :End ◆ :Repeat Above ◆Text:None, Window, Bottom : :Done!
    In your screenshot you have a loop that never ends, since there is no Break Loop command.

  2. $gameMap._interpreter.wait($gameVariables.value(27)) makes the current non-Parallel event wait (if there is one running). Parallel events each run through their own interpreter; there's no convenient way to reference "this" event/interpreter from within a move route.
Alright, so, it works like a treat on the NPC racers, so thank you very much for that. If possible, I would like to apply a similar wait effect to the player between each of their steps. Any ideas on how I could pull that off?

I have a separate event on the field that makes it basically impossible for the player to move in specific directions, to keep them moving in the right direction. I was thinking I could maybe insert something in there that would delay player movement, but as it so happens, "player.wait" doesn't exist. Or, if it does exist, it doesn't in the way I had been wanting to use it.

This link should help with the offscreen movement, but I think it could probably cause some lag if overused.

Otherwise, it looks like caethyril has you covered on the rest. Good luck!

As for you, yeah, that works perfectly, and it'll only be used for this racing game, so I don't foresee any lag issues coming up. Thanks for pointing that out.
@Skijarama: this is not a fixed reference (I should've mentioned that earlier). It returns the current context, which differs for move commands and event commands:
  • Script (in move route): this = event interpreter.
  • Script (in event): this = character being moved.
You have this.wait(Math.randomInt(8) + 0) in a non-move route Script command, so that's telling the interpreter (event) to wait 0~7 frames, not the player. Since the event is Parallel, that will allow the player to move on their own during those 0~7 frames. Possible solutions:
  1. Move that this.wait(etc) script call into the move route, like you've done with the other events.

    [Edit: actually it should be like this._waitCount = Math.randomInt(8) for move route waiting.]

  2. Alternatively, you could try changing the player-movement event to Autorun trigger. Normal player controls are suspended when a non-Parallel event is running, but only one non-Parallel event can run at a time. Autorun is the usual trigger for cutscenes etc.
(Also, you don't need it here, but you can reference the player map character as $gamePlayer.)

Edit: and the thing Ben pointed out is that an event's autonomous move route stops processing if the event is sufficiently far off-screen. If you're doing all movement via Set Movement Route commands, though, then I don't think that'll be a problem here.
@Skijarama: this is not a fixed reference (I should've mentioned that earlier). It returns the current context, which differs for move commands and event commands:
  • Script (in move route): this = event interpreter.
  • Script (in event): this = character being moved.
You have this.wait(Math.randomInt(8) + 0) in a non-move route Script command, so that's telling the interpreter (event) to wait 0~7 frames, not the player. Since the event is Parallel, that will allow the player to move on their own during those 0~7 frames. Possible solutions:
  1. Move that this.wait(etc) script call into the move route, like you've done with the other events.

  2. Alternatively, you could try changing the player-movement event to Autorun trigger. Normal player controls are suspended when a non-Parallel event is running, but only one non-Parallel event can run at a time. Autorun is the usual trigger for cutscenes etc.
(Also, you don't need it here, but you can reference the player map character as $gamePlayer.)

Edit: and the thing Ben pointed out is that an event's autonomous move route stops processing if the event is sufficiently far off-screen. If you're doing all movement via Set Movement Route commands, though, then I don't think that'll be a problem here.

The problem is that using the "this.wait" in the move routes didn't actually work. When I tried that it caused my game to crash. Unless I am fundamentally misunderstanding some part of the instructions. I might be. I'm not as tech-savvy as I ought to be for this kinda stuff and it is 3:00 AM as I am writing this.

My brain is mulch right now.
The problem is that using the "this.wait" in the move routes didn't actually work. When I tried that it caused my game to crash.
What was the error message? Or do you mean it literally just crashed to desktop? 😅

Maybe you made a typo when entering it the first time, e.g. This.wait(Math.randomInt(8)) will cause a "cannot read property wait of undefined" error because This (capital T) doesn't exist. Similarly if you typed math (small m). 😴

Don't feel the need to reply immediately if you need some sleep!
What was the error message? Or do you mean it literally just crashed to desktop? 😅

Maybe you made a typo when entering it the first time, e.g. This.wait(Math.randomInt(8)) will cause a "cannot read property wait of undefined" error because This (capital T) doesn't exist. Similarly if you typed math (small m). 😴

Don't feel the need to reply immediately if you need some sleep!
I don't foresee it as a typo, as I literally Copy pasted what you put here. Caused a crash in the move route in the event programming, but not when it was just in the general event code. As for the error message, I'll let you see it in a video.

Here is a video on dropbox I recorded demonstrating the error I recieve, and I will attach a screenshot of how the code is setup that creates it.
Oh wow, sorry! Yea, that was my mistake, the script call should be like this for move routes (character wait):

this._waitCount = Math.randomInt(8)
I.e. "set this character's wait time to 0~7 frames". Turns out it's correct in the Script Call List, I just goofed here. I'll correct my earlier post, too. 😴

Also, for future reference, you could have just quoted the "this.wait is not a function" message rather than posting a 10+ MB video (took me a minute to actually buffer that on my WiFi).
Oh wow, sorry! Yea, that was my mistake, the script call should be like this for move routes (character wait):

this._waitCount = Math.randomInt(8)

I.e. "set this character's wait time to 0~7 frames". Turns out it's correct in the Script Call List, I just goofed here. I'll correct my earlier post, too. 😴

Also, for future reference, you could have just quoted the "this.wait is not a function" message rather than posting a 10+ MB video (took me a minute to actually buffer that on my WiFi).

I work better with visual aids. Also my brain is still mulch because I slept like garbanzo beans and JUST got up. The vid's my bad on that one. ^^; I'll try the updated code and get back to you.

EDIT: Mmkay, so, it works. Mostly. However it DOES come with the problem that if I run the player racer control script as Autorun, it seems to interfere with the finish line script, making the player just go over the line without updating their lap counter. I can get around this by just not having it run as autorun and overcompensate on the number of frames their movement gets delayed by, but eh.

Either way, the two main issues I was having that I opened this thread for have been more or less sorted out. From this point it will likely just be fine-tuning on my end. All I really need to work out now is how to make one-use powerups work... and I already kinda know how to do that because I did it with a three-charge sprint function. Just need to find the time (and patience) to nail down the specifics.

Original thread: https://forums.rpgmakerweb.com/threads/making-an-event-wait-a-number-of-frames-equal-to-a-randomized-variable.146974/