Opening a GUI

Started by dodgethesnail, Tue 23/01/2007 08:07:23

Previous topic - Next topic

dodgethesnail

I have been looking around in the forum and in help files and FAQs and all that, but I still can't find a good solid answer to my very simple question. Maybe I'm bad at searching, but I tried. If there is a thread that explains this, please by all means direct me to it. Anyways:

How do I make a key on the keyboard (such as F5) bring up a GUI?

I have a GUI ready, I just can't figure out how to bring it up (and close it) using a key on the keyboard.

I'm sure this must sound very noobish, but some help would be very much appreciated.
Thanks

Dan_N

Look for on_key_press in the manual, that should take you to the event, and there's a link there to the codes for each key. Then all you have to do is
Code: ags

 if (on_key_press(keycode)==1)
 {
   gGUI.Visible=true;
 }

Barbarian

#2
For my current project, I have a little "Health" Gui in which when the player presses the "H" key it will toggle On or Off the Gui. Ã, I use a Global Interger (variable) to check if the Gui is currently on or off, and therefore if the gui is off, it will turn it on, and if it's on it will turn it off.

From the AGS editor, click on the "Script" option from the tool-bar, then select "on_key_press". From there you can add in your code to check for key presses and what to do when a certain key has been pressed.

In my case, and using the "H" key, H having an ascii code of "72", the code looks something like this:

Code: ags
 Ã, if ((IsKeyPressed(72)==1) && (GetGlobalInt(1)==1)) {GUIOff(7); SetGlobalInt(1,0);}
else if ((IsKeyPressed(72)==1) && (GetGlobalInt(1)==0)) {GUIOn(7); SetGlobalInt(1,1);} 


I used the now obsolete "GUIOff" and "GUIOn" commands in this case, but for current scripting commands it would use "gHealth.Visible = true;" or "gHealth.Visible = false;" would turn on or off my "Health" GUI accordingly.

Ã,  Each key has it's own "ascii code" number. So, for example, the "Space Bar" would have a number of "32". You can see an ascii-chart by pressing the link below:
http://www.2dadventure.com/ags/ascii_chart.gif

The code for the "F5" key should be:
Code: ags
if (keycode==363)
, but as AGS already by default brings up a Save Gui when pressed, you would need to change the part of that code that immediately follows that according to what you want to happen.

Good luck.

*Opps. Noticed someone else posted a message while I was making mine, but as I gave a bit more detail (such as how to toggle on and off a GUI and the ascii chart), thought I'd still go ahead and post my message as well.Ã,  ;)
Conan: "To crush your enemies, see them driven before you, and to hear the lamentation of the women!"
Mongol General: "That is good."

Blade of Rage: www.BladeOfRage.com

Khris

#3
@Dan: No.

Go to the menu: Script -> on_key_press

This will open the global script at:
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==17)  QuitGame(1);   // Ctrl-Q
  if (keycode==363) SaveGameDialog();   // F5
  if (keycode==365) RestoreGameDialog();  // F7
  if (keycode==367) RestartGame();  // F9
  if (keycode==434) SaveScreenShot("scrnshot.bmp");  // F12
  if (keycode==19)  Debug(0,0);  // Ctrl-S, give all inventory
  if (keycode==22)  Debug(1,0);  // Ctrl-V, version
  if (keycode==1)   Debug(2,0);  // Ctrl-A, show walkable areas
  if (keycode==24)  Debug(3,0);  // Ctrl-X, teleport to room
  // ADD HERE
}


Now add e.g.
Code: ags
  if (keycode==364) {  // F4
    if (gGui_name.Visible) gGui_name.Visible=false;
    else gGui_name.Visible=true;
  }

This code will toggle the GUI's visibility when you press F4.

EDIT: If the GUI is set to Pop-up modal (opening it will pause the game), the code has to go before the if (IsGamePaused()-line!

PS: It's not necessary to use a GlobalInt, there's IsGuiOn when using old-style code.

Barbarian

Either way would work. I used a GlobalInt in my case is all.
Conan: "To crush your enemies, see them driven before you, and to hear the lamentation of the women!"
Mongol General: "That is good."

Blade of Rage: www.BladeOfRage.com

dodgethesnail

Okay I'm trying it your way KhrisMUC.
My code looks like this now:
(MAINGUI is the name of my GUI by the way)

Code: ags

#sectionstart on_key_press  // DO NOT EDIT OR REMOVE THIS LINE
function on_key_press(int keycode) {
  // called when a key is pressed. keycode holds the key's ASCII code  if (keycode==364) {  // F4
  if (keycode==364) {  // F4
    if (MAINGUI.Visible) MAINGUI.Visible=false;  //THIS IS LINE 141 (see my problem below)
    else MAINGUI.Visible=true;
  }
  if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
  if (keycode==17)  QuitGame(1);   // Ctrl-Q
  if (keycode==363) SaveGameDialog();   // F5
  if (keycode==365) RestoreGameDialog();  // F7
  if (keycode==367) RestartGame();  // F9
  if (keycode==434) SaveScreenShot("scrnshot.bmp");  // F12
  if (keycode==19)  Debug(0,0);  // Ctrl-S, give all inventory
  if (keycode==22)  Debug(1,0);  // Ctrl-V, version
  if (keycode==1)   Debug(2,0);  // Ctrl-A, show walkable areas
  if (keycode==24)  Debug(3,0);  // Ctrl-X, teleport to room
  // ADD HERE
}


Okay, so I put that little bit of code before the IsGamePaused line like you said, because my GUI is set to popup modal. But now I get this little error message when I try to test the game:

Error (line 141): expected semicolon after ')'

What is wrong with line 141? How do I fix it?


Barbarian

Maybe the code is getting "confused" because,I think, you're using "MAINGUI" in your code, where it should be "gMaingui"Ã,  (that is if Maingui is indeed the name of your GUI you're trying to use). You need the little "g" in front of the GUI name. So, change all your intances of "MAINGUI" over to "gMaingui".

Well, I could be wrong, but from a quick glance at your code that's what I think could be the problem.
Conan: "To crush your enemies, see them driven before you, and to hear the lamentation of the women!"
Mongol General: "That is good."

Blade of Rage: www.BladeOfRage.com

Ashen

Yup, you need to use gMaingui.Visible, not MAINGUI.Visible.
MAINGUI is the Script-name for the GUI, and is actually an int value. You could use gui[MAINGUI], but it's easier to use the Script-O-Name (gMaingui) which is the pointer type the GUI.Visible property uses. I'm not sure why it gives that particular error, but that's the cause of it.

Also, keycode == 364 would be F6, not F4. Don't know if this is just a typo, or you changed your mind about what key to use and didn't change the comment, but I thought it might save you some frustration to know why pressing F4 didn't do anything :)
I know what you're thinking ... Don't think that.

Khris

Right, my mistake, F4's keycode is 362.

A complete reference can be found in the manual.

dodgethesnail

Great! It works just fine now! Thank you everybody!!

SMF spam blocked by CleanTalk