keypress = enable (show) GUI

Started by td, Tue 30/05/2006 15:06:08

Previous topic - Next topic

td

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

Ubel

#1
It should be done like this:
Code: ags

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


I changed the () marks to []

Ashen

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.:
Code: ags

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)
}
I know what you're thinking ... Don't think that.

td

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?

Gilbert

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.

alkis21

#5
EDIT: TD's problem's solved.

td

Yay! Now it's work! Thanks to dudes and cyborg! :)

td

Wait the minute!!! Some mistake there. I suppose ur was right
alkis21!!!

Ubel

Code: ags

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. :)

alkis21

Does Pablo's suggestion work, TD?

td

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...

Ashen

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.
I know what you're thinking ... Don't think that.

Khris

If the GUI is set to Pop up modal, on_key_press won't get called while it is open, AFAIK.

Ashen

#13
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):

Code: ags

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.
I know what you're thinking ... Don't think that.

alkis21

#14
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):

Code: ags

int KeyHeld=0;
function repeatedly_execute_always() {
  if ((KeyHeld) && IsKeyPressed(KeyHeld)) return;
  KeyHeld=0;
  if (IsKeyPressed(' ')) {
    gui[3].Visible = (!gui[3].Visible);
    KeyHeld = ' ';
    }
}

Ubel

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.

td

#16
Seems it work, Pablo! :) (SOLVED)

SMF spam blocked by CleanTalk