Page 1 of 1

Wait for input for a certain amount of time?

Posted: Sat Dec 17, 2011 10:30 pm
by Tesselode
So I want to have a function like this:

wait_for_input(time)

Where if you push a button within that amount of time it returns true, otherwise when the timer is up it returns false. Is there any way I can do this? If it helps, I am using hump.timer in my game.

Re: Wait for input for a certain amount of time?

Posted: Sat Dec 17, 2011 10:45 pm
by Robin
No, because such a function would suspend execution, thus stopping the game completely, ignoring all input and drawing nothing to the screen.

You'll want to rework your control flow to make it work. Probably use hump.timer.add(amount_of_time, cancelfunction).

[Response]ButtonWaiter Prototype

Posted: Sun Dec 18, 2011 5:34 am
by rhezalouis
Hi tesselode,
Tesselode wrote:if you push a button within that amount of time it returns true, otherwise when the timer is up it returns false.
To do that, you need to track the time that has passed and check the pressed keys. I guess the most easy and flexible way to manage these variables are by using an object/prototype (in Lua, an object is simply a table :P). In the attached LÖVE file, you could see a from-scratch sample of such object. The object buttonWaiter didn't return true or false as you requested though, instead, it receives a callback function that would be called if the key-you're-waiting-for is pressed when the object is in wait (isActive). In the sample callback (shout()), you could see that it toggles a boolean flag that could be used to substitute the returned value you mentioned ^^.

Check this out :ultrahappy:
tesselode-buttonWaiter.love
LÖVE0.7.2
(1.45 KiB) Downloaded 41 times
P.S. Anyway, if you still want to implement this requirement using functions, you could create a function that returns a newly defined waiting_for_input function and its upvalues. There might be some difficulties to control the upvalues as the load, update, draw and pressedkey-check are done in different callbacks. Be careful it might end up being un-tabled version of that object/table implementation :roll:; I guess the logic for functions should be quite different from tables.

Re: Wait for input for a certain amount of time?

Posted: Sun Dec 18, 2011 6:30 am
by Tesselode
Don't worry, I already figured out something.