Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: td on Tue 30/05/2006 15:06:08

Title: keypress = enable (show) GUI
Post by: td on Tue 30/05/2006 15:06:08
Can't find the answer...
Just try set up the key (Space) button => enable (show) GUI. But i cant.

I paste code in Global script:

if (IsKeyPressed(32) == 1)
gui(3). Visible = true ;

What i do wrong ???
Title: Re: keypress = enable (show) GUI
Post by: Ubel on Tue 30/05/2006 15:27:08
It should be done like this:

if (IsKeyPressed(32) == 1) {
Ã,  gui[3].Visible = true;
}


I changed the () marks to []
Title: Re: keypress = enable (show) GUI
Post by: Ashen on Tue 30/05/2006 15:37:46
Also, where exactly in the Global Script is it?
IsKeyPressed I think needs to be use in repeatedly_execute to work properly. You could also add to on_key_press, e.g.:

function on_key_press(int keycode) {
  // called when a key is pressed. keycode holds the key's ASCII code
  if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
  if (keycode == 32) gui[3].Visible = true;
  // Other keycode conditions (Quit, Save, Load, Inventory, etc)
}
Title: Re: keypress = enable (show) GUI
Post by: td on Wed 31/05/2006 07:04:43
But i can do this in global script :
if (keycode==8)  gui[3].Visible = true;

Function is work...and now gui3 must disappear when "space" pressed again. Global script to allow do this?
Title: Re: keypress = enable (show) GUI
Post by: Gilbert on Wed 31/05/2006 09:33:43
if (keycode==8)  if (gui[3].Visible) gui[3].Visible = false;
else gui[3].Visible = true;

Though this way the gui might flash on and off if you keep the key pressed, I'm too lazy to work something about this though.
Title: Re: keypress = enable (show) GUI
Post by: alkis21 on Wed 31/05/2006 09:48:52
EDIT: TD's problem's solved.
Title: Re: keypress = enable (show) GUI
Post by: td on Wed 31/05/2006 09:51:44
Yay! Now it's work! Thanks to dudes and cyborg! :)
Title: Re: keypress = enable (show) GUI
Post by: td on Wed 31/05/2006 10:18:48
Wait the minute!!! Some mistake there. I suppose ur was right
alkis21!!!
Title: Re: keypress = enable (show) GUI
Post by: Ubel on Wed 31/05/2006 10:53:11

if (keycode==8) {
Ã,  if (gui[3].Visible==true) {
Ã,  Ã,  gui[3].Visible=false;
Ã,  Ã,  return; } //"return" stops running any more functions
Ã,  else {
Ã,  Ã,  gui[3].Visible=true;
    return; }
}


This should work. :)
Title: Re: keypress = enable (show) GUI
Post by: alkis21 on Wed 31/05/2006 10:58:09
Does Pablo's suggestion work, TD?
Title: Re: keypress = enable (show) GUI
Post by: td on Wed 31/05/2006 15:56:50
Wah! Dosn't work... :(
GUI apeear when push the button but not disappear when push the button again...

...well, guess so better to set other button for disappearing GUi...
Title: Re: keypress = enable (show) GUI
Post by: Ashen on Wed 31/05/2006 16:05:04
Pablo's code should work... Can you post your on_key_press code?
What key do you want to turn the GUI on/off? You originally said Space, but now you're using keycode 8, which is Backspace.
Title: Re: keypress = enable (show) GUI
Post by: Khris on Wed 31/05/2006 16:32:14
If the GUI is set to Pop up modal, on_key_press won't get called while it is open, AFAIK.
Title: Re: keypress = enable (show) GUI
Post by: Ashen on Wed 31/05/2006 16:42:24
Ah, that's true. In that case, you will need to use IsKeyPressed like you originally had, and check it in repeatedly_execute_always, as this runs when Popup Modal GUIs are visible. You might also want to use a variable to stop it 'blinking' on and off if the key is held (what Gilbot said), e.g. (untested):


int KeyHeld;

function repeatedly_execute_always () {
// Make rep_ex_always if there isn't one, or paste this in if there is:
  if (IsKeyPressed (8)) {
    if (KeyHeld == 0) {
      if (gui[3].Visible==true) {
        gui[3].Visible=false;
      }
      else {
        gui[3].Visible=true;
      }
      KeyHeld = 1;
    }
  }
  else KeyHeld = 0; // Reset it when key released.
}


EDIT:
Corrected conditions. This version DOES work, honest, but Pablo's module (or at least hthe way he's modified on_key_press) might be better.
Title: Re: keypress = enable (show) GUI
Post by: alkis21 on Wed 31/05/2006 18:58:21
Ashen, I don't think your code would stop the blinking either.
EDIT: I just tested it and it doesn't.

Here's how I did it (I have a GUI that pops up when Space is pressed too):


int KeyHeld=0;
function repeatedly_execute_always() {
  if ((KeyHeld) && IsKeyPressed(KeyHeld)) return;
  KeyHeld=0;
  if (IsKeyPressed(' ')) {
    gui[3].Visible = (!gui[3].Visible);
    KeyHeld = ' ';
    }
}
Title: Re: keypress = enable (show) GUI
Post by: Ubel on Wed 31/05/2006 19:23:45
I would make a new module and put a on_key_press() function inside it, without the line that blocks keypresses while the game is paused. In my opinion this would be the most non-confusing way of doing this. Actually I just made one. Just import this module into AGS from the Module Manager.

http://koti.mbnet.fi/pabsoft/ags/keypress_module.scm

There.
Title: Re: keypress = enable (show) GUI
Post by: td on Thu 01/06/2006 08:38:11
Seems it work, Pablo! :) (SOLVED)