Placing a delay between key presses?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Placing a delay between key presses?

Post by BoopDeePoop »

I'm attempting to place delay between when you can press a certain key. I want it so that when you press the space bar, the key does what it's assigned to do, and in order for the game to take any more input from the space bar, you must wait x amount of seconds before it will actually press space again. I'm trying not to use any libraries for this so if someone could help that'd be great. Everytime I try nothing works.
Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Re: Placing a delay between key presses?

Post by Kyle »

Try a timer.

Code: Select all

local time = 0
local enabled = true

function love.update(dt)
time = math.max(0, time - dt)
if time <= 0 then enabled = true end
end

--when you do your keypress:
time = delay
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Placing a delay between key presses?

Post by Plu »

Remember in the above example you'd also need to set enabled to false in the keypress, and you'd have to check if enabled == true before executing the keypress code.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Placing a delay between key presses?

Post by BoopDeePoop »

I get main.lua 40: attempt to perform arithmetic on global "time" (nil value)

Here's my .Love
Attachments
game.love
(208.93 KiB) Downloaded 188 times
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 1:41 am, edited 1 time in total.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Placing a delay between key presses?

Post by BoopDeePoop »

I usually always put variables into load(). Guess I shouldn't then. Still, the error is gone now but I'm still able to press the space bar as many times as I want.

Code: Select all

function control_list(key)

--Only affects game if in 'Playing' state
		if gamestate == "playing" then
	if enabled == true then
	if key == " " then
		time = 3
		enabled = false
		phrase = math.random(1,20)
		end
	end
end

--Only affects game if in 'Menu' state
		if gamestate == "menu" then
	end

--Only affects game if in 'Options' state
		if gamestate == "options" then
	end

--General Controls, work in any state.

	if key == "escape" then
		love.event.quit()
	end
	if key == "1" then
		gamestate = "menu"
	elseif key == "2" then
		gamestate = "playing"
	elseif key == "3" then
		gamestate = "options"
	elseif key == "4" then
		gamestate = "controls"
	end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Placing a delay between key presses?

Post by Robin »

BoopDeePoop wrote:I usually always put variables into load(). Guess I shouldn't then.
You can, just don't declare them as local in love.load(). ;)
Help us help you: attach a .love.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: Placing a delay between key presses?

Post by BoopDeePoop »

Robin wrote:
BoopDeePoop wrote:I usually always put variables into load(). Guess I shouldn't then.
You can, just don't declare them as local in love.load(). ;)
Thanks for the note! I'm still having a problem with the code though. I'm able to press the key as many times as I want without delay.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Placing a delay between key presses?

Post by Zeliarden »

make time = 0 global ( by removing local), that should fix it
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 153 guests