🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Strange issue with input on game engine.

Started by
3 comments, last by rafaelsantana 4 years, 3 months ago

Hello,

I am trying to make a FPS game with a second game engine project that I have been working on. However, I noticed that when I play the game, I can only press down one key at a time, whereas a normal game allows you to hold down multiple keys at the same time (like “Up" and “Left”). This one, on the other hand, only lets me press “Up”, and when I press “Left”, the character would stop moving forward, and only move to the side.

Here is my walking forward code:

…
#define UP 0x4800
…
 int main(void)
 {
 …
     unsigned ch_val; //this variable will return what key is pressed.
     if (bioskey(1))
    {
	ch_val = getkey();
       if (ch_val==ESC)
       {
	 done = 1;
       }

      switch(ch_val)
	{ 
    case UP:    if ((com_device == 'n') || (com_device == 'm'))com_tx(PORT, 'a');
       if (com_device=='e')
       {
  data_packet[20] = 'a';//networking BS
  send_pkt (interrupt_number,data_packet, 64); //networking BS
       }
       result = movePOV(player,PlayerAngle,16); //this just increases an X value
       if (result == POV_OBJECT)
  check_objects();
       moves++; //more networking crap
       break;
       }
       …
       }

I don't usually have this issue with bioskey, but I am in kind of a hurry to finish this part of the project, so I might be missing something. This is also one of my first attempts at making my own framework (with a keyboard). Maybe the way I set up the "getkey()" might be giving me errors.

I did not implement the left code, as it is just a simple increment to a variable.

I am not sure what is happening, and I have tried changing it to “if”and “while” statements, which I knew would not work, but, hey, it is worth giving them a chance.

Thanks for helping me in advance!

-Ryan

Advertisement

Are you saving the states of your input? A key-down event should only occurre once and after some time there will arrive the key-up. You have to save a state that your key was held down and then update the position in another place, your game loop for example until the state is removed.

I don't know what you are doing there to be honest but it sounds as if a state overrides the other and your update code is in the wrong place

@undefined After a very brief pause, the repeat “feature” begins on held down keys for my keyboard. I just cannot hold two keys down at the same time because the repeat stops for the first key and starts for the second key.

@shaarigan Thanks, man! That was the problem. I was putting it in the wrong loop. It was simply a brackets issue (my code is messy).

Here is how I did it but there may be a better way,

    // 'b_jmpkeydown' and 'b_jmpcheck' are used to make
    // the player sprite jump only once per key press.
    if(HIWORD(GetKeyState(0x44)))
    {
        b_jmpkeydown = true; // key is down
    }
    else // we can jump again
    {
        b_jmpkeydown = false; // key is up
        b_jmpcheck = false; // reset for next pass
    }

    // left arrow key
    if(HIWORD(GetKeyState(VK_LEFT)))
    {
        // do stuff
    }

    // right arrow key
    if(HIWORD(GetKeyState(VK_RIGHT)))
    {
    	// do stuff
    }

    // d key; jump
    if(HIWORD(GetKeyState(0x44)))
    {
    	// if jump key is still down, abort
        if(b_jmpcheck && b_jmpkeydown)return;

        // request a new jump sequence
	
        b_jmpcheck = true;
    }

This is very Windows specific, so it many not be a solution that is cross-platform compatible.

This topic is closed to new replies.

Advertisement