Page 1 of 2

waitkey()

Posted: Sat Mar 14, 2009 11:09 am
by phoku
Hi there.

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

-phoku

Re: waitkey()

Posted: Sat Mar 14, 2009 1:23 pm
by Skofo
Can you explain what it'd do?

Re: waitkey()

Posted: Sat Mar 14, 2009 1:51 pm
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?

Re: waitkey()

Posted: Sat Mar 14, 2009 7:12 pm
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?

Re: waitkey()

Posted: Sat Mar 14, 2009 7:58 pm
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.

Re: waitkey()

Posted: Sun Mar 15, 2009 7:57 am
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

Re: waitkey()

Posted: Sun Mar 15, 2009 10:08 am
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.

Re: waitkey()

Posted: Sun Mar 15, 2009 10:18 am
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:

Re: waitkey()

Posted: Sun Mar 15, 2009 10:33 am
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.

Re: waitkey()

Posted: Sun Mar 15, 2009 10:37 am
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 :-)