waitkey()

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

waitkey()

Post by phoku »

Hi there.

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

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

Re: waitkey()

Post by Skofo »

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

Re: waitkey()

Post by phoku »

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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: waitkey()

Post by bartbes »

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?
User avatar
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

Re: waitkey()

Post by phoku »

Not hard, but inconvenient.
I assumed that someone had already written something along those lines.

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
(122.57 KiB) Downloaded 218 times
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: waitkey()

Post by appleide »

This should work providing time doesn't advance during the draw() function; i.e draw() only draws and does nothing else.
I've uploaded a demo.
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.
(2.35 KiB) Downloaded 208 times
User avatar
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

Re: waitkey()

Post by phoku »

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.

Thank you for your time.
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: waitkey()

Post by appleide »

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.

Thank you for your time.
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. :halloween:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: waitkey()

Post by bartbes »

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.
User avatar
phoku
Prole
Posts: 6
Joined: Sat Mar 14, 2009 10:35 am

Re: waitkey()

Post by phoku »

*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
(122.57 KiB) Downloaded 218 times
Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests