Using the same button twice

Started by TDBFW1, Sat 23/09/2017 19:27:08

Previous topic - Next topic

TDBFW1

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

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?

TDBFW1

I've just realised something else I could try:
Code: ags

    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?

Crimson Wizard

#2
Normally this should work:

Code: ags

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

TDBFW1

It still doesn't work. When the GUI is open, the key doesn't seem to register.

Crimson Wizard

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

TDBFW1

No textboxes or modules. I don't know what's wrong...

TDBFW1

So If I change the GUI to normal, initially off it works. No idea why it doesn't work when paused. Thanks anyway

Crimson Wizard

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

if (IsGamePaused())
   return;

?

SMF spam blocked by CleanTalk