How do you...

Started by
15 comments, last by Fractal 22 years, 6 months ago
...get the programme to perform a particular operation WHENEVER a particular key is pressed. This is for my text-based game, if that''s any help. Could I be any more dumber? (What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
Advertisement
Simple, you have your main loop block (wait) for input, such as this:

  int main(){	int value = 0;	while (cin >> value )	{		//This is where you''d parse the value and 				//determine what is to be done with a valid		//command.  In this example I''m just		//outputting the value to the screen.		cout << value << endl;		if ( value == 0 )			break;	}	return 0;}  


You''ll notive that inside the while loop is where you''d process your character''s move, or whatever.

What you''d want to do for a text adventure game is have the main loop spin like this, waiting for command input from the user and then you send whatever data you read into a "command parser" which would determine what the command meant, if anything. Once you know what the command means you can process the results of the command execution.

RandomTask
I don''t think that was quite the reply that I wanted. I''ll give an example of what I want done.

You''re playing the game.
And at ANY point in the game, if you press ''S'', it will save the game as it is.
Could that sort of code work for that?

Could I be any more dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
If you are coding under dos you will have to substitute the keyboard interrupt with one of your own.

------------------------------------------------------
Cuando miras al abismo el abismo te devuelve la mirada.
F. Nietzsche
ok, This'll work for you. I didn't test it so there will probably by parse and syntax errors so bear with me. Now, I use linux/unix and that is what I program for but I think that these functions are standard c.

What this does now is provide a method of NON-blocking IO. Select listens on stdin (standard in (where you type)) and if it doesn't find user input within a certain, specified amount of time, it exits.

If it does find input it will let you know, as in ret_val will return positive. Once you know that you have input you use fgets on stdin to get the information from the screen input file descriptor. Then you want to parse the data and determine what needs to be done. I showed the save game and end game scenario right here.

    #include <unistd.h>#include <sys/time.h>#include <sys/types.h>#include <stdio.h>int main( void ){        fd_set rfds;        struct timeval tv;        int retval;        //256 is the longest length input string we'll take and the plus one        //is for the null terminator on the end \0        char inputString[256 + 1];        bool keepLooping = true;        //This sets us up to watch standard in (fd 0) to see when it has input        //data.        FD_ZERO(&rfds);        FD_SET(0, &rfds);        //Wait for up to 1 second for user input.        tv.tv_sec = 1;        tv.tv_usec = 0;	//main game loop        while ( keepLooping )        {                ret_val = select( 1, &rfds, NULL, NULL, &tv );                if (ret_val)                {                        //There is data on the file descriptor,                        //better collect it.                        fgets(inputString,257,stdin);                        //Now lets process the data the user                        //entered and check to see if we're to                        //end the game.                        keepLooping = parseInputString( inputString, 257 );                }                //Do other game processing;                UpdateMap();		EnemyAI();                MoveMonsters();                CheckTime();                //etc        }}bool parseInputString( char *, int size ){        if( strcmp( inputString, "S" ) == 0 ||                strcmp( inputString, "s" ) == 0 )        {                SaveGame();        }        else if( strcmp( inputString, "Q" ))        {                //End the game                return false;        }        else        {                //Look for other input                ;        }        return true;}  


I hope this helps. This should allow you to capture input characters when they happen, but won't make your program stop the processing of the entire game engine waiting for input (block on IO.)

Cheers,
RandomTask


Edited by - RandomTask on October 16, 2001 6:44:43 PM
register your own hook in windows, I forget what actual function is, something like, wait a minute ... searching msdn http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msaa/msaaccrf_55m3.asp
Sadly RandomTask, that code does not work, as parts of it I have been told are unix/linux only. Any other suggestions?

Could I be any more dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
here try this, its probably loaded with syntax errors, but hopefully you will get the idea:

char value
while (running)
{
cin >> value;

case ''s''
save game()
{
//save game
}

hope this helps, although its probably oaded with errors

"people think Pandoras box was evil, it wasnt, the box aint nothin but a box"
"people think Pandoras box was evil, it wasnt, the box aint nothin but a box"
here try this, its probably loaded with syntax errors, but hopefully you will get the idea:

char value
while (running)
{
cin >> value;

case ''s''
save game()
{
//save game
}

hope this helps, although its probably oaded with errors

"people think Pandoras box was evil, it wasnt, the box aint nothin but a box"
"people think Pandoras box was evil, it wasnt, the box aint nothin but a box"
More like:
// main game loopwhile(not_done){  // other stuff...  //*** input section ***/  if(kbhit())  {    // find out which key was hit    switch(getch())    {    case ''s'': // upper or lower case; use fall-through nature of case statement    case ''S'':      save_game();      break;    // other case labels...    }  }  // other stuff...} 

This is DOS/Windows Console stuff, and suffers from the buffer problems of the default keyboard interrupt vector. For superior resolution and capacity you''ll need to replace the interrupt handler... or switch to DirectInput.

Also note that this means you can''t use the ''s'' key for anything else in the game, including names, commands... Given how frequently we use ''s'' in English (plurality), maybe you should make your save key F7 or Ctrl-S or so.

This topic is closed to new replies.

Advertisement