Page 1 of 1
Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 8:31 pm
by noahdavis319
Hello all,
I am looking for someway to wait x amount of seconds before continuing something. What I am trying to do, is have the user type some text into my prompt, and when he/she presses the 'backspace' key, remove 1 letter from the string (Already done), but if they user is still holding the key down, after 1 second, continue removing 1 letter from the string 1 at a time. While i'm here, what is the unicode or key name for the 'enter' key? Or how could I tell if the user pressed the 'enter' key.
Code: Select all
function love.load()
text={}
str1=""
end
function love.keypressed(key,unicode)
if unicode>37 and unicode<127 then
table.insert(text,string.char(unicode)) --Add last used character to text-list
elseif key=="backspace" then
table.remove(text,#text) --Remove last inserted character from text-list
end
end
function love.draw()
str1=""
for _,n ni pairs(text) do
str1=str1..n
end
love.graphics.printf(str1,0,0,800)
end
Re: Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 9:03 pm
by T-Bone
noahdavis319 wrote:Hello all,
I am looking for someway to wait x amount of seconds before continuing something. What I am trying to do, is have the user type some text into my prompt, and when he/she presses the 'backspace' key, remove 1 letter from the string (Already done), but if they user is still holding the key down, after 1 second, continue removing 1 letter from the string 1 at a time. While i'm here, what is the unicode or key name for the 'enter' key? Or how could I tell if the user pressed the 'enter' key.
Code: Select all
function love.load()
text={}
str1=""
end
function love.keypressed(key,unicode)
if unicode>37 and unicode<127 then
table.insert(text,string.char(unicode)) --Add last used character to text-list
elseif key=="backspace" then
table.remove(text,#text) --Remove last inserted character from text-list
end
end
function love.draw()
str1=""
for _,n ni pairs(text) do
str1=str1..n
end
love.graphics.printf(str1,0,0,800)
end
It's definately possible to achieve the effect you're looking for, but I don't know if a wait()-function would solve that. Wouldn't it block the entire thread?
Re: Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 9:04 pm
by Helvecta
Re: Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 9:05 pm
by Nixola
It's even possible without having to do it manually:
love.keyboard.setKeyRepeat
P.S: Avoid
waiting and
sleeping; as T-Bone says, it would block the entire program, it would actually freeze while it's waiting
Re: Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 10:15 pm
by noahdavis319
I'm not sure. I just want the function to wait a second.
Re: Does love have a 'wait()'?
Posted: Fri Oct 12, 2012 10:32 pm
by Inny
To answer op's questions, setKeyRepeat is how you should handle users typing and holding keys. Also, the enter key is named "return".
To answer the thread's name, Love does has a wait function, but it's so that a thread can block for a specified amount of time. In love.run, waiting is needed before running the main loop again. Threads can call wait as well if there's no useful work to be done. You normally shouldn't use it.
As for getting your game to wait for a specified amount of time, there's several ways to do this, but the best way to do it is to have a timer, meaning a variable that increases by a time step until it's done. A small code example of such a thing is this:
Code: Select all
-- timers.lua
local M = {}
local timers = {}
local callbacks = {}
function M.start(name, delay, callback)
timers[name] = delay
callbacks[name] = callback
end
function M.update(dt)
for name, v in pairs(timers) do
v = v - dt
if v <= 0 then
local fn = callbacks[name]
M.stop(name)
fn()
else
timers[name] = v
end
end
end
function M.stop(name)
timers[name] = nil
callback[name] = nil
end
return M
Code: Select all
-- main.lua
local timers = require 'timers'
function love.init()
timers.start("example", 5, function() print("Example event") end)
end
function love.update(dt)
timers.update(dt)
end
I baked this code out in like a minute, mostly as an example of how you'd do timed events. For a better timer system, use Kikito's cron.lua:
https://github.com/kikito/cron.lua