Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: LeChuck on Sun 28/09/2008 00:10:10

Title: Quick question regarding On_Key_Press (SOLVED)
Post by: LeChuck on Sun 28/09/2008 00:10:10
Alright, in my game the player can use TAB to open or close the inventory list. Problem is, if I hold it down it flickers like hell. How to I make it wait until the button is released without using Wait(1);?

Here's my code:


if (keycode == 9 || keycode == 54) { // TAB or 6

  if (IsGUIOn(LUGGAGE) && IsGUIOn(OPTIONS)==false) { // Inventory GUI is off, so turn it ON.
   
PlaySound(358); //bings
StopMoving(EGO);
GUIOn(INVENTORY);
GUIOff(LUGGAGE); // button that opens inventory if clicked on
     
        if (GetCursorMode()==eModeWalk) SetCursorMode(eModeUse);
   }

   else if (IsGUIOn(INVENTORY)==true) { // Inventory GUI is on, so turn it OFF.
     
PlaySound(359);
        GUIOff(INVENTORY);
        EnableCursorMode(0);
        GUIOn(LUGGAGE); // button that opens inventory if clicked on
        SetGlobalInt(1, 0);
   }
}


Im guessing the clue is the IsKeyPressed function, but I'm not sure what to make of it.
Title: Re: Quick question regarding On_Key_Press
Post by: monkey0506 on Sun 28/09/2008 10:07:49
You're using some legacy scripting there (this scripting is designed for AGS 2.62 and earlier), so my first question would be which version of AGS you're using.

Really it doesn't make a difference to the actual solution, but if you're using AGS 2.7 or higher, it's recommended to upgrade to the new object-oriented style scripting (there's a broader base of help available for the current AGS standard ;)).

Basically you'll want to do something like this:

// top of global script
int keyHeld = 0;

// inside of on_key_press
if (((keycode == 9) || (keycode == 54)) && (!keyHeld)) { // make sure the key isn't already being held
  keyHeld = keycode; // store the key we pressed
  // if (IsGUIOn(LUGGAGE)...
  ...
}

function repeatedly_execute() {
  if ((keyHeld) && (!IsKeyPressed(keyHeld)) keyHeld = 0; // if the key has been released, reset our variable
}


Actually...I'm thinking that on_key_press shouldn't automatically repeatedly call the code if you hold the key. It may just be the fact that it's 4 in the morning...but...anyway...
Title: Re: Quick question regarding On_Key_Press
Post by: LeChuck on Wed 01/10/2008 03:52:19
Quote from: monkey_05_06 on Sun 28/09/2008 10:07:49
You're using some legacy scripting there (this scripting is designed for AGS 2.62 and earlier), so my first question would be which version of AGS you're using.

I hear this in every thread I post, but I've been working on this game for a few years now and it's too much work changing every bit of code. Using 2.72.


Quote from: monkey_05_06 on Sun 28/09/2008 10:07:49
Basically you'll want to do something like this:

Yeah, with a few modifications, it works great! Thank you! :o
Title: Re: Quick question regarding On_Key_Press (SOLVED)
Post by: monkey0506 on Wed 01/10/2008 19:38:54
Glad you got it working. Sorry about any confusion regarding your scripting. ;)