## waitkey()

General discussion about LÖVE, Lua, game development, puns, and unicorns.
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

### waitkey()

Hi there.

How would you go about implementing a waitkey function?
With coroutines this should be possible. But how?

-phoku
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

### Re: waitkey()

Can you explain what it'd do?
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

### Re: waitkey()

Sure.

The function does not return until the player presses a key.

The basic assumption is of course, that this is somehow embedded in scheduling framework,
which loads and runs functions as coroutines. A target would a cutscene loader and runner.

Example:

Code: Select all

   -- This is a cutscene script.
display_text( "Welcome to the game. Press a key to proceed." )
waitkey()
display_text( "Thanks.")

That's a rough example. Also, for example traditionaly rougelikes are written in a more
serial fashion, querying user input in a blocking fashion.

Clear now?
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: waitkey()

Blocking would result in not drawing anymore.
But, how hard would it be to only do something to the game data in the keypressed callback?
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

### Re: waitkey()

Not hard, but inconvenient.

I attached a very bad version of the waitkey() 'challenge' 8-)

The image is from http://steampunkwallpaper.com/?paged=3, CC derivative.
Attachments
waitkey.love
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

### Re: waitkey()

This should work providing time doesn't advance during the draw() function; i.e draw() only draws and does nothing else.
Everything in this post is in public domain.
updated function and demo:

Code: Select all

function waitKey()
local callbacks={
keypressed=keypressed,
keyreleased=keyreleased,
mousereleased=mousereleased,
mousepressed=mousepressed,
joystickpressed=joystickpressed,
joystickreleased=joystickreleased,
update=update
};
update=function() end;
keyreleased=function() end;
mousereleased=function() end;
mousepressed=function() end;
joystickpressed=function() end;
joystickreleased=function() end;

keypressed=function(key)
update=callbacks.update;
keypressed=callbacks.keypressed;
keyreleased=callbacks.keyreleased;
mousereleased=callbacks.mousereleased;
mousepressed=callbacks.mousepressed;
joystickpressed=callbacks.joystickpressed;
joystickreleased=callbacks.joystickreleased;
if type(waitkeypressed)=="function" then
waitkeypressed(key);

end
end
end

Attachments
waitkey.zip
waits key when rectangle at x=100, then adds key number to rectangle's y value.
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

### Re: waitkey()

That's not, you know, blocking. Pushing and popping callbacks as a way of handling game state sure is one way ...

Well, I assumed that someone surely had written a system to do this using coroutines,
which I could shamelessly ... learn from. So back to the design board it is, for me.

appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

### Re: waitkey()

phoku wrote:That's not, you know, blocking. Pushing and popping callbacks as a way of handling game state sure is one way ...

Well, I assumed that someone surely had written a system to do this using coroutines,
which I could shamelessly ... learn from. So back to the design board it is, for me.

If you wanted cutscenes while waiting for a key press then just add your cutscene code into the empty functions assigned to callbacks inside the waitkey function.
It's not blocking but I think Im still achieving the same effect.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: waitkey()

Blocking + games = no drawing = bad

Basically all I have to say about it, appleide's solution might be best, because that leaves the draw function working.
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

### Re: waitkey()

*cough*

This is possible with coroutines. I have attached the proof of concept before.
The question was not whether it's possible, but how to do it good :-)
Attachments
waitkey.love