Does love have a 'wait()'?

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
noahdavis319
Prole
Posts: 2
Joined: Fri Oct 12, 2012 8:24 pm

Does love have a 'wait()'?

Post 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

User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Does love have a 'wait()'?

Post 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?
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Does love have a 'wait()'?

Post by Helvecta »

"Bump." -CMFIend420
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Does love have a 'wait()'?

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
noahdavis319
Prole
Posts: 2
Joined: Fri Oct 12, 2012 8:24 pm

Re: Does love have a 'wait()'?

Post by noahdavis319 »

I'm not sure. I just want the function to wait a second.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Does love have a 'wait()'?

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest