Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: thom0512 on Sun 10/08/2008 03:21:25

Title: Text box automatically resets itself through GUI buttons? (SOLVED)
Post by: thom0512 on Sun 10/08/2008 03:21:25
Hello, everyone.  I am new to AGS and have started working on my own game.  I have always made a consistent effort to search through the manual and on the forum to find answers to my questions before I ask, and up to now, that's what I have done.  I am stumped, however, on this certain portion I have been trying to complete in my game.

I have created a GUI of a keypad.  I would like for this keypad to be able to unlock a door in the same room once a six-digit code has been entered, which is "233524".  The GUI is composed of nine buttons, which contain the numbers 1-9, and a text box, which displays the numbers that have been punched in.  My dilemma is this: I am aware that, through the keyboard, you can manually enter characters in the text box.  However, I would like to include the option of utilizing the GUI's buttons and pushing the buttons to display numbers.  I have figured this out, but whenever I push a new number, it overrides the old number that was previously in the text box.  Obviously, I want to script this so that pressing buttons on the keypad will be the same as manually typing in the code on the keyboard.  This is the code that I have regarding the buttons so far, when the GUI is visible:

function Keypad1_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="1";
}

function Keypad2_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="2";
}

function Keypad3_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="3";
}

function Keypad4_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="4";
}

function Keypad5_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="5";
}

function Keypad6_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="6";
}

function Keypad7_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="7";
}

function Keypad8_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="8";
}

function Keypad9_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text="9";
}

I have searched countless times in the manual and on the forum attempting to solve this.  I do apologize if this question has already been answered in a previous thread.  Again, I have been working on this game for a couple of months and have always looked up a question I have had.  This specific problem has just stumped me, though.  Any help would be greatly appreciated, and if I have been unclear in any way, please let me know and I'll try to elaborate.  Thanks!
Title: Re: Text box automatically resets itself through GUI buttons?
Post by: Makeout Patrol on Sun 10/08/2008 08:00:24
The reason that it's replacing the input is because that script is replacing the content of that String rather than adding on to it. I don't have the manual handy, but you're going to need to write something to this effect:

function Keypad1_OnClick(GUIControl *control, MouseButton button)
{
Keypadscreen.Text = Keypadscreen.Text + "1";
}

If this doesn't work, the + sign is the problem. Look through the manual to see if you can find the operator that allows you to join Strings together.
Title: Re: Text box automatically resets itself through GUI buttons?
Post by: Mazoliin on Sun 10/08/2008 08:39:20
Quote from: Makeout Patrol on Sun 10/08/2008 08:00:24
If this doesn't work, the + sign is the problem. Look through the manual to see if you can find the operator that allows you to join Strings together.

Keypadscreen.Text = Keypadscreen.Text.AppendChar('1');
Title: Re: Text box automatically resets itself through GUI buttons?
Post by: Khris on Sun 10/08/2008 10:40:51
Additionally, you'll probably want the leftmost digit to disappear if the player enters a seventh digit, correct?

Plus, the whole OnClick business can be simplified greatly:
Every GUIControl has a unique ID, starting at 0. If you create/edit the buttons so that their label is the same as their ID, you'll only need one OnClick to cover all of them:

function Keypad_OnClick(GUIControl *control, MouseButton button) {
  if (button != eMouseLeft) return;  // only react to left-clicks
  String s = Keypadscreen.Text;
  if (s.length == 6) s = s.Substring(1, 5);  // cut off leftmost digit
  Keypadscreen.Text = String.Format("%s%d", s, control.ID);      // add the button's ID to the display

  if (Keypadscreen.Text == "233524") CallRoomScript(1);  // unlock door
}

Now enter "Keypad_OnClick" into each button's OnClick field.

You might want to use a label rather than a textbox to display the code, btw, to prevent the player from typing into it.

In the room script, add this function:
function on_call(int p) {
  if (p==1) {
    // this gets called by   CallRoomScript(1);
    // show animation of opening door, etc.
  }
}
Title: Re: Text box automatically resets itself through GUI buttons? (SOLVED)
Post by: thom0512 on Wed 13/08/2008 03:40:36
Thanks so much.  I appreciate all of the help.  It answered my question, but unfortunately, more things came up as I was editing around with things.  I'll put those aside for now to see if I can work them out on my own.  Again, thank you!  I will mark this thread as solved.
Title: Re: Text box automatically resets itself through GUI buttons?
Post by: Khris on Wed 13/08/2008 12:33:09
You'll have to edit the subject line of your first post in order to put "solved" in the topic list.
Title: Re: Text box automatically resets itself through GUI buttons? (SOLVED)
Post by: thom0512 on Thu 14/08/2008 01:56:31
Ha ha, my mistake.  Thanks for catching that.