Wait for input for a certain amount of time?

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
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Wait for input for a certain amount of time?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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).
Help us help you: attach a .love.
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[Response]ButtonWaiter Prototype

Post 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 148 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.
Last edited by rhezalouis on Sun Dec 18, 2011 6:46 am, edited 2 times in total.
Aargh, I am wasting my posts! My citizenshiiiip... :o
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

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

Post by Tesselode »

Don't worry, I already figured out something.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 3 guests