# How do you restrict the left click rate in the game engine?

In my game I have an action performed whenever the left mouse button is pressed. Is there a way to restrict how fast the player can click? Right now the event will trigger as many times as they can press the button, but I would like to put a cap on that so that the action can't be spammed so many times per second.

• Are you using logic block exclusively, or are you also doing some scripting? – Matt Jan 10 '14 at 17:25
• I have a ray sensor and mouse sensor fed into a python brick for the action, and the the mouse sensor also fed into Nand-->sound to play a sound effect. – user3015600 Jan 10 '14 at 17:29
• What if I give the object a boolean property set to true and have python check to see if it is true. If it is make it false and set back to true after the a python timer has gone after a certain time. Does python have its own built in timer? If so, how accurate would this method be? – user3015600 Jan 10 '14 at 17:52
• Using timer.sleep() uses to much processing. – user3015600 Jan 10 '14 at 19:40
• Don't use timer.sleep() It doesn't actually use processor, it yields it's processor time to the OS either for the specified amount of time, or until your process receives a signal that it needs to process. This is not what you want. I'll add more detail to my answer... – Matt Jan 10 '14 at 19:49

I'm afraid I'm not familiar with the game engine side of Blender, but the principle would be to have a timer (or other condition) that gets checked on each mouse click. If the timer has expired (or other condition is met), then the click triggers whatever action it's liked to, and resets the timer. If the timer has not yet expired, then the click is ignored.

If you doing some scripting, then you want to use lastClickTime = time.time() to store the time of the first click. Then use time.time() - lastClickTime > 1 as your condition. Time.time() will give you the current time (in seconds since 1970). If the current time minus your stored time is greater than 1, then at least one second has elapsed.

So (SUUUUPER pseudo-code... this won't actually do anything):

#somewhere else you initialized lastClickTime to zero
OnClick():

if time.time() - lastClickTime > 1:
#do the click thing and...
lastClickTime = time.time()

else:
#do nothing..


So you handle every single click you get, it's just that you don't have to DO anything when you get a click. If your timer has expired, do what the click is supposed to do and reset the timer. If not, then just do nothing.

Note that time() is a member of the time external module, so you have to import the time module in order to use the time() member.

Time.time() only gives 1 second resolution. There are ways to get sub-second resolution, but the principle is the same. You'll just use getRealtimeClock() or something like that, instead of time.time().

It is also possible to use timer properties. After playing around with Cobra's method, I found that this works reliably and allows you to use the full accuracy of the timer property.

Fire = cont.sensors["Fire"]
fireRate = own["fireRate"]
if fireRate >= 0 and Fire.positive:
# Do not set fireRate = -1 That does not work.
own["fireRate"] = -1
#anything you want to limit the action of

• Worked perfectly, except it seemed that I had to import time. Just a side note. – user3015600 Jan 17 '14 at 18:05
• Quite right. Edited answer. – Matt Jan 17 '14 at 18:10

You can create a timer property in the logic editor panel and name it whatever you want. Say - "dTimer". Now when your game runs it will run. And in your python script do like this -

if mouse_click.positive and own["dTimer"] >= 2: #2 seconds
#do whatever you want
own["dTimer"] = 0 #Reset the counter to zero


As soon as you reset it to 0, it will start the counting from 0 again. You can debug the property if you want.