Help with Always Keypresses (SOLVED)

Started by monkey0506, Wed 15/02/2006 02:40:35

Previous topic - Next topic

monkey0506

During my game, keypresses should not be ignored during speech, so I'm looking to an alternative to on_key_press.  I have the following script:

Code: ags
#sectionstart on_key_press_always  // DO NOT EDIT OR REMOVE THIS LINE
function on_key_press_always(int keycode) {
  // called when a key is pressed. keycode holds the key's ASCII code
  if ((keycode >= 97) && (keycode <= 122)) keycode = CharToUpper(keycode);
  if ((IsGamePaused()) && (keycode != 32) && (keycode != 366) && (keycode != 23) &&
  (keycode != 3) && (keycode != 17) && (keycode != 324) && (keycode != 434) &&
  (!gPaused.Visible) && (!gRestart.Visible) && (!gWin.Visible) && (!gQuit.Visible) &&
  (keycode != 45) && (keycode != 61) && (!gVolume.Visible))
    keycode = 0;
  if (keycode==363) SaveGameDialog(); // F5
  if (keycode==365) RestoreGameDialog(); // F7
  if (keycode==367) RestartGame(); // F9
  if (keycode == 434) {
    SaveScreenShot(String.Format("guibuilder%03d.bmp", Screenshot));  // F12
    Screenshot++;
    }
  if (keycode==19) Debug(0,0); // Ctrl-S, give all inventory
  if (keycode==22) Debug(1,0); // Ctrl-V, version
  if (keycode==1) Debug(2,0); // Ctrl-A, show walkable areas
  if (keycode==24) Debug(3,0); // Ctrl-X, teleport to room
  if (keycode == 32) gPaused.Visible = (!gPaused.Visible);
  if (keycode == 366) gRestart.Visible = true;
  else if ((gRestart.Visible) && (keycode == 89)) RestartGame();
  else if (gRestart.Visible) gRestart.Visible = false;
  if (keycode == 23) gWin.Visible = true;
  else if ((gWin.Visible) && (keycode == 89)) {
    gWin.Visible = false;
    Display("YOU WIN!"); // pseudo-interaction
    // go to -you-win-room-
    }
  else if (gWin.Visible) gWin.Visible = false;
  if ((keycode == 3) || (keycode == 17) || (keycode == 324)) gQuit.Visible = true;
  else if ((gQuit.Visible) && (keycode == 89)) QuitGame(0);
  else if (gQuit.Visible) gQuit.Visible = false;
  if (keycode == 13) {
    if (GetLocationType(mouse.x, mouse.y) == eLocationNothing) {
      GUI* guiat = GUI.GetAtScreenXY(mouse.x, mouse.y);
      if (guiat != null) {
        GUIControl* controlat = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
        if (controlat != null) interface_click(guiat.ID, controlat.ID);
        else on_mouse_click(eMouseLeft);
        }
      else on_mouse_click(eMouseLeft);
      }
    else on_mouse_click(eMouseLeft);
    }
  if (keycode == 9) {
    if (GetLocationType(mouse.x, mouse.y) == eLocationNothing) {
      GUI* guiat = GUI.GetAtScreenXY(mouse.x, mouse.y);
      if (guiat != null) {
        GUIControl* controlat = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
        if (controlat != null) interface_click(guiat.ID, controlat.ID);
        else on_mouse_click(eMouseRight);
        }
      else on_mouse_click(eMouseRight);
      }
    else on_mouse_click(eMouseRight);
    }
  if ((!gPaused.Visible) && (!gRestart.Visible) && (!gWin.Visible) && (!gQuit.Visible) &&
  (!gVolume.Visible)) {
    if (keycode == CharToUpper(eLucasKeycode_Give)) {
      mouse.Mode = eModeUseGive;
      GiveMode = true;
      }
    if (keycode == CharToUpper(eLucasKeycode_Open)) mouse.Mode = eModeOpen;
    if (keycode == CharToUpper(eLucasKeycode_Close)) mouse.Mode = eModeClose;
    if (keycode == CharToUpper(eLucasKeycode_Pickup)) mouse.Mode = eModePickup;
    if (keycode == CharToUpper(eLucasKeycode_Lookat)) mouse.Mode = eModeLookat;
    if (keycode == CharToUpper(eLucasKeycode_Talkto)) mouse.Mode = eModeTalkto;
    if (keycode == CharToUpper(eLucasKeycode_Use)) {
      mouse.Mode = eModeUseGive;
      GiveMode = false;
      }
    if (keycode == CharToUpper(eLucasKeycode_Push)) mouse.Mode = eModePush;
    if (keycode == CharToUpper(eLucasKeycode_Pull)) mouse.Mode = eModePull;
    }
  if ((keycode == 45) || (keycode == 61)) {
    gVolume.Visible = true;
    Wait(5);
    if ((sldVolume.Value > 27) && (sldVolume.Value <= 99) && (keycode == 45))
      sldVolume.Value -= 9;
    if ((sldVolume.Value >= 27) && (sldVolume.Value < 99) && (keycode == 61))
      sldVolume.Value += 9;
    SetMusicMasterVolume(sldVolume.Value);
    }
  }
#sectionend on_key_press_always  // DO NOT EDIT OR REMOVE THIS LINE

int GetKeyPress() {
  int i = 0;
  while (i < 434) {
    if ((IsKeyPressed(405)) || (IsKeyPressed(406))) {
      int j = 65;
      while (j <= 90) {
        if (IsKeyPressed(j)) return j - 64;
        j++;
        }
      }
    if (IsKeyPressed(407)) {
      int j = 65;
      while (j <= 90) {
        if (IsKeyPressed(j)) return j + 236;
        j++;
        }
      }
    if (IsKeyPressed(i)) return i;
    i++;
    }
  return 0;
  }

int RunKeypressTimer = 0;

#sectionstart repeatedly_execute_always  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute_always() {
  // put anything you want to happen every game cycle here
  if (RunKeypressTimer == 0) {
    on_key_press_always(GetKeyPress());
    RunKeypressTimer = 2;
    }
  else RunKeypressTimer--;
  }
#sectionend repeatedly_execute_always  // DO NOT EDIT OR REMOVE THIS LINE


The code works okay for single keypresses (I wouldn't say great because it's still responds more than I would like it to, but setting RunKeypressTimer to 3 makes it too unresponsive), but key combinations are giving me some problems.

For example, I press 324, and the QUIT GUI turns on, but it instantly goes back off again...even if I mash the buttons and release as quick as I possibly can...and even if I increase the timer to make it less responsive.

Does anyone have any suggestions on how I can fix this?

Gilbert

Would it be possible if there're copy of such keypress checks in the original on_key_press() ?

If say, for example, both o_k_p() and o_k_p_a() checks for 324 to be pressed and display/remove the quit dialog, BOTH will be executed.

monkey0506

Actually I have o_k_p() commented out.

Gilbert

I think I see your problem now.

 if ((keycode == 3) || (keycode == 17) || (keycode == 324)) gQuit.Visible = true;
Ã,  else if ((gQuit.Visible) && (keycode == 89)) QuitGame(0);
Ã,  else if (gQuit.Visible) gQuit.Visible = false;

Think about it, the red line would be executed even when (keycode==0).
Since the check would be done every several loops, even when no key is being pressed, in the beginning of the o_k_p_a() codes, if the player presses a "useless" key or does not press any kay at all you set keycode to 0, since there're nothing to do in these cases I'd recommend changing the "keycode=0" to "return 0" instead.

monkey0506

Hmmm...interesting point.  It didn't pose any threats in o_k_p()...:D.

Adding a "(keycode != 0)" check in there seems to have done it...Thanks Gilbot!

SMF spam blocked by CleanTalk