[Solved] Check to see if a player is spamming a button?

I searched online and on this forum so if this was asked and answered I am clearly in need of an eye check up heheh.

I want to know if there is a way I can have my game check to see if an input is being quickly spammed.

For context, I use nearly all of Yanfly Engine Plugins, a few Hime Plugins, and a few Galv Plugins. For this mechanic, I am using Yanfly Picture Common Events and HIME Toggleable States Plugins.

I have a game mechanic where the player can swap gender presentations at will via a button on the hud. It flashes the screen, plays an animation and swaps the Sprites, Faces, and Battlers of the main character.

I was playtesting my game with a friend, and my friend jokingly wanted me to spam the button to have it swap over and over and over, which is hilarious because it felt like we were playing a game of gender roulette, which is totally fine, but I had the idea of having the game check to see if this was happening and drop an easter egg of the main character complaining about having to swap so quickly one after another.

Is there a way to do this? I specifically want to check for quick spamming of the button, not the action of it turning on and off.

Thanks so much!

~NyallWynn

Plugins:

HIME Toggleable State: https://himeworks.com/2015/11/toggle-able-states/
YEP: Picture Common Events: http://www.yanfly.moe/wiki/Picture_Common_Events_(YEP)
The simplest way would probably be to have the button also increase some variable by one, and have a parallel common even that constantly decreases it by one if it's greater than zero, then waits ~30 frames, than repeats itself. So if that variable ever becomes high enough (say, over 5 or 10) you'll know that the player must be pressing the button rather quickly.
@Mac15001900's idea is probably the best one. If you're not afraid of using JavaScript, you can make the event a little bit simpler by avoiding the parallel common event. The key is to use JavaScript's setTimeout() function, which allows you to execute code after some time has passed.

So whenever the player presses the gender swap button, you'd:
  1. Increment the counter.
  2. Use setTimeout() to decrement the counter after some time has passed.
  3. Check if the counter is above the threshold.
    • If it is, drop the easter egg.

The script in question would be:
JavaScript:
const delay = 5000;
const varId = 42;
setTimeout(() => {
    const counter = $gameVariables.value(varId);
    $gameVariables.setValue(varId, counter - 1);
}, delay);
In this example, the counter is stored in Variable #0042 and the delay is 5000 milliseconds (5 seconds).

Note that there's no need to check if the counter is above 0 with this method, because the counter will only be decremented after it's been incremented.

The only downside is that there's a potential bug, where if the player manages to save before the delay is over, the next time they load, the counter would be above 0 and stay there. If, for example, your condition is "press the button twice within 5 seconds" and the player manages to save before the 5 seconds are over, then the next time they load the game, they will get the easter egg every time they swap genders.

This can be fixed by either setting the delay low enough (setTimeout() continues to run even when the player is in the menu) or by using a plugin to reset the counter to 0 whenever the game has been loaded.
@Mac15001900This seems super simple, I wish I had thought of that!

@nolonar.117999 I don't know a lot of java but I do know enough to not get totally lost. So if I were to say, have Variable 7 "button" increase by 1 whenever the button gets pressed, and say have it go down every 1000 or so miliseconds, and the variable hit say 5 or 10, that would work?
You could have a setInterval at the start of your code, that resets the counter every X millis, and check up the counter in the input update.

Or instead of a setInterval you can use the frame var (Graphics.frameCount) and check every X frames

Example, put this in the input update function (check every 60 frames)
Code:
if(Graphics.frameCount % 60 === 0) count = 0;
I would strongly recommend sticking with the game loop for this, rather than trying to use an external timer like setTimeout or setInterval. Otherwise you might desync from the game's input checks, particularly with the engine's fluid timestep implementation (variable frame times). 😴

In case it helps, I posted this button-mash example in another thread:
You can use a (self) switch to track whether the button was pressed or not last frame. Here's an example I copied from one of my scrap projects:
Spoiler
◆Control Variables:#0019 Mash = 30 ◆Control Variables:#0020 Time = 300 ◆Loop ◆If:Button [OK] is pressed down ◆Wait:1 frame ◆ :Else ◆Break Loop ◆ :End ◆ :Repeat Above ◆Loop ◆If:Button [OK] is pressed down ◆Control Switches:#0020 ButtonDown = ON ◆ :Else ◆If:ButtonDown is ON ◆Control Switches:#0020 ButtonDown = OFF ◆Control Variables:#0019 Mash -= 1 ◆ :End ◆ :End ◆If:Mash ≤ 0 ◆Comment:Complete! ◆Break Loop ◆ :Else ◆If:Time ≤ 0 ◆Comment:Time up! ◆Break Loop ◆ :Else ◆Comment:Continue. ◆Control Variables:#0020 Time -= 1 ◆Wait:1 frame ◆ :End ◆ :End ◆ :Repeat Above
The first loop is to make sure the button is unpressed before starting, the second one is for the actual mashing.

Alternatively, as implied, you could use Conditional Branch -> Script:

Input.isTriggered("ok")
This should be true if the "ok" button is pressed this frame but wasn't pressed the previous frame.

Also, in case you haven't seen it, this tutorial may be helpful:
The idea here is to check the button state (pressed or not) every frame for the duration of the input interval. With a switch to remember the button state last check, that lets you count distinct button presses over the interval. This example starts the count at 30 and -1 each time "OK" is released; it would be easy to replace that with 0 and +1 instead.
It works pefectly, thank you everyone!!

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/solved-check-to-see-if-a-player-is-spamming-a-button.152840/