Jump to content

  • Log In with Google      Sign In   
  • Create Account


How do you...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 October 2001 - 09:18 AM

...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"?)

Sponsor:

#2 RandomTask   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 October 2001 - 09:40 AM

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

#3 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 October 2001 - 09:49 AM

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"?)


#4 Abominacion   Members   -  Reputation: 198

Like
Likes
Like

Posted 16 October 2001 - 10:10 AM

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

#5 RandomTask   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 October 2001 - 11:41 AM

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

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 16 October 2001 - 12: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

#7 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 October 2001 - 09:02 AM

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"?)


#8 kevinivek   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 04:25 PM

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"

#9 kevinivek   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 October 2001 - 04:25 PM

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"

#10 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 19 October 2001 - 06:40 PM

More like:

// main game loop
while(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.

#11 Greg Rosenblatt   Members   -  Reputation: 219

Like
Likes
Like

Posted 22 October 2001 - 02:54 AM

To avoid that problem of not being able to reuse the s key for typing, you can set up a state variable that can be toggled by some other key (perhaps the one right above tab), which determines whether you''re in "typing" mode, or if you''re in the regular game mode. It will allow you to have multiple functionality for any key, determined by the state you''re in.

To give a very hacked (but understandable) example, and borrowing from Oluseyi''s code:

  
// some state variable declared

bool typing = false;
// main game loop

while(not_done)
{
// other stuff...

//*** input section ***/

if(kbhit())
{
if (typing)
FillString(); // typing handler

else
{
// find out which key was hit

switch(getch())
{
case ''`'':
typing = true;
break;
case ''s'': // upper or lower case; use fall-through

case ''S'':
save_game();
break;
// other case labels...

}
}
}
// other stuff...}


The typing handler would systematically get keyboard inputs and begin filling an input buffer, and would fire off some "string entered" event once the user hit enter (or whatever you find appropriate). You could also allow the user to cancel his typing request by hitting some other key, which would erase the buffer and not send a string out. Once either occurs, typing would be set to false once again.

"Don''t be afraid to dream, for out of such fragile things come miracles."

#12 RandomTask   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 October 2001 - 06:49 AM

Hey guys,

I think he''s looking for a solution that isn''t blocking on IO. That is what the select solution did. It provided a non-blocking solution to the problem. The cin solution is a blocking solution. Is kbhit() blocking? If not, it''d work great for what he wants.

RandomTask


#13 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 22 October 2001 - 07:33 AM

kbhit() return true if the keyboard has been hit. It doesn''t wait for input and it returns immediately. It''s perfect for elementary response (it''s useless if there may be simultaneous keypresses).

#14 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 October 2001 - 09:45 AM

Now I know I may sound like a complete newb saying this (and I am ), but what''s the "while(not_done)" for? By this I mean what''s the "not_done" for? I realise that you expect me to put something else here, but what?

Could I be any more dumber?
(What do you mean, "No"?)


#15 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 22 October 2001 - 09:56 AM

bool not_done = true;

When you''re ready to exit the program, set not_done to false. Writing

while(!done)

has the same effect as writing

while(not_done)

if you declare them as opposites.

#16 onlinegmz   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 October 2001 - 10:37 AM

by chance are you a DigiPen Student? Check out MSDN search for ReadConsoleInput or PeekConsoleInput. These will help a lot, if you use these your input is stored in a buffer you can check in some sort of game loop. This is also great for console graphics for graphics search for GetConsoleScreenBufferInfo using these you can create back buffers for awesome ASCII animations!!!! If you are infact a DigiPen student stop by Euclid if you have any questions.

PEACE

#17 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 22 October 2001 - 10:46 AM

Also GetAsyncKeyState()works well, it gets the state of a specified key (up or down), it returns immediatly and doesn''t require a messagepump, message handler or any other win32 specific stuff.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS