Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: TDBFW1 on Sat 23/09/2017 19:27:08

Title: Using the same button twice
Post by: TDBFW1 on Sat 23/09/2017 19:27:08
I'm trying to open a GUI with the "n" button. Once the GUI is open, I want to be able to close the GUI with the "n" button again. However, if I try to do something like this:

if ((IsKeyPressed(eKeyN)) &&
(!GUIOpen))
{
gNotebook.Visible=true;
GUIOpen=true;
}
else if (GUIOpen)
{
gNotebook.Visible=false;
GUIOpen=false;
}
}


it doesn't work. How do I fix this and where do I put the code?
Title: Re: Using the same button twice
Post by: TDBFW1 on Sat 23/09/2017 19:38:28
I've just realised something else I could try:

    if ((NotebookOpen) &&
  (IsKeyPressed(eKeyN)))
  {
    gNotebook.Visible=false;
    NotebookOpen=false;
  }
  if ((!NotebookOpen) &&
  (IsKeyPressed(eKeyN)))
  {
    gNotebook.Visible=true;
    NotebookOpen=true;
  }


However, the GUI won't close when "n" is pressed. The GUI pauses game when shown. Any help?
Title: Re: Using the same button twice
Post by: Crimson Wizard on Sat 23/09/2017 19:49:23
Normally this should work:

Code (ags) Select

function on_key_press(eKeyCode key)
{
    if (key == eKeyN)
    {
        gNotebook.Visible = !gNotebook.Visible; // switch GUI visibility to reverse of what it is now
    }
}


You can put this in GlobalScript.asc. If the script already contains function "on_key_press", then just add this new code into it.

Regarding your original code, you do not need to have separate NotebookOpen variable, because gNotebook.Visible already tells you if GUI is visible (remember, you can both set and get its value).
Title: Re: Using the same button twice
Post by: TDBFW1 on Sat 23/09/2017 19:59:00
It still doesn't work. When the GUI is open, the key doesn't seem to register.
Title: Re: Using the same button twice
Post by: Crimson Wizard on Sat 23/09/2017 20:23:24
Quote from: TDBFW1 on Sat 23/09/2017 19:59:00
It still doesn't work. When the GUI is open, the key doesn't seem to register.

Do you have any TextBoxes on the GUI? I think they catch all the key input.
Other than that, I cannot think of anything but maybe another script module overrides keypresses when it's open, if you have any.
Title: Re: Using the same button twice
Post by: TDBFW1 on Sat 23/09/2017 20:29:31
No textboxes or modules. I don't know what's wrong...
Title: Re: Using the same button twice
Post by: TDBFW1 on Sat 23/09/2017 21:01:29
So If I change the GUI to normal, initially off it works. No idea why it doesn't work when paused. Thanks anyway
Title: Re: Using the same button twice
Post by: Crimson Wizard on Sat 23/09/2017 21:05:34
Quote from: TDBFW1 on Sat 23/09/2017 21:01:29
So If I change the GUI to normal, initially off it works. No idea why it doesn't work when paused.

Does your on_key_press function has something like this:
Code (ags) Select

if (IsGamePaused())
   return;

?