←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

[REQUEST] Wait for keypress or time

LoganDark's Photo LoganDark 25 Apr 2016

Are requests even allowed in this section?

I'd like a function that waits for a user to press a key, but after a specific amount of seconds, if the user hasn't pressed a key (it should skip instantly if you do) it skips automatically. Is that possible?

I have good knowledge with CC Lua, so feel free to explain it as technical as you want.
Edited by LoganDark, 25 April 2016 - 07:35 PM.
Quote

masterdisasterHD's Photo masterdisasterHD 25 Apr 2016

try this:

local timeout = 3

function ontimer()
sleep(timeout)
end

function onkey()
os.pullEvent('key')
end

parallel.waitForAny(ontimer, onkey) --This waits for either of the 2 functions to finish before continuing
--Further processing below this line

Edited by masterdisasterHD, 25 April 2016 - 07:39 PM.
Quote

LoganDark's Photo LoganDark 25 Apr 2016

View PostmasterdisasterHD, on 25 April 2016 - 07:37 PM, said:

try this:

local timeout = 3

function ontimer()
sleep(timeout)
end

function onkey()
os.pullEvent('key')
end

parallel.waitForAny(ontimer, onkey) --This waits for either of the 2 functions to finish before continuing
--Further processing below this line

Thanks!
Quote

LDDestroier's Photo LDDestroier 25 Apr 2016

I actually made the help screen of my QuickDraw game so that it slowly prints the help text, but if you press a button, it instantly displays the text. It's neato
Quote

LoganDark's Photo LoganDark 25 Apr 2016

View PostLDDestroier, on 25 April 2016 - 07:43 PM, said:

I actually made the help screen of my QuickDraw game so that it slowly prints the help text, but if you press a button, it instantly displays the text. It's neato

I just had to use parallel.waitForAny.
Quote

Lyqyd's Photo Lyqyd 25 Apr 2016

Moved to Ask a Pro.
Quote

LoganDark's Photo LoganDark 25 Apr 2016

View PostLyqyd, on 25 April 2016 - 09:25 PM, said:

Moved to Ask a Pro.

Better to lock too, since my problem was solved. It also introduced me to parallel.waitForAny.
Quote

Lupus590's Photo Lupus590 26 Apr 2016

Ask a pro is usually not locked as it allows others with a similar problem to post additional questions
Edited by Lupus590, 26 April 2016 - 11:33 AM.
Quote

Bomb Bloke's Photo Bomb Bloke 26 Apr 2016

Other users are usually encouraged to make their own threads, but it's indeed recommended for posters to keep all their questions about a given project in the one place.

In any case, there's seldom good reason to actually lock a thread. It's a fairly heavy-handed way of saying "drop this subject".
Quote