How do you...
...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"?)
Simple, you have your main loop block (wait) for input, such as this:
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
|
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"?)
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
------------------------------------------------------
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.
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
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.
|
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
October 16, 2001 06:03 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 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"
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"
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:
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.
// 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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement