Jump to content




[REQUEST] Wait for keypress or time



8 replies to this topic

#1 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 25 April 2016 - 07:34 PM

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.


#2 masterdisasterHD

  • Members
  • 172 posts
  • LocationThe Netherlands

Posted 25 April 2016 - 07:37 PM

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.


#3 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 25 April 2016 - 07:40 PM

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!

#4 LDDestroier

  • Members
  • 1,095 posts
  • LocationACDC Town

Posted 25 April 2016 - 07:43 PM

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

#5 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 25 April 2016 - 07:45 PM

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.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,464 posts

Posted 25 April 2016 - 09:25 PM

Moved to Ask a Pro.

#7 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 25 April 2016 - 10:31 PM

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.

#8 Lupus590

  • Members
  • 2,027 posts
  • LocationUK

Posted 26 April 2016 - 08:37 AM

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.


#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 26 April 2016 - 08:47 AM

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".





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users