(SOLVED)Help creating a telephone

Started by Joey, Sun 23/02/2014 01:17:57

Previous topic - Next topic

Joey

I have created a GUI with numbers 0-9, a textbox to display the number, a call button, and an exit button. I need help with creating a script to check the text inside of the textbox to see if the right number was dialed. My script to use buttons to enter the code in also stopped working but I didn't change anything. Here is an example of script for the buttons

Code: ags

function PhoneButton2_OnClick(GUIControl *control, MouseButton button)
{
  TextBox1.Text.AppendChar('2');
}


Anyway, what I really need help on is creating a script that checks if the number entered in is correct and then starts a dialog when the call button is pressed.

Mandle

#1
Quote from: Joey on Sun 23/02/2014 01:17:57
I have created a GUI with numbers 0-9, a textbox to display the number, a call button, and an exit button. I need help with creating a script to check the text inside of the textbox to see if the right number was dialed. My script to use buttons to enter the code in also stopped working but I didn't change anything. Here is an example of script for the buttons

Code: ags

function PhoneButton2_OnClick(GUIControl *control, MouseButton button)
{
  TextBox1.Text.AppendChar('2');
}


Anyway, what I really need help on is creating a script that checks if the number entered in is correct and then starts a dialog when the call button is pressed.

I would create a Global Variable called something like: PhoneNumberSequence. Make it an int set initially to 1.

Now let's say the phone number is : 234 (short for easy example's sake)

Code: ags

function PhoneButton2_OnClick(GUIControl *control, MouseButton button)
{
  TextBox1.Text.AppendChar('2');
if (PhoneNumberSequence == 1)
{
PhoneNumberSequence ++;
}
}

function PhoneButton3_OnClick(GUIControl *control, MouseButton button)
{
  TextBox1.Text.AppendChar('3');
if (PhoneNumberSequence == 2)
{
PhoneNumberSequence ++;
}
}

function PhoneButton4_OnClick(GUIControl *control, MouseButton button)
{
  TextBox1.Text.AppendChar('4');
if (PhoneNumberSequence == 3)
{
PhoneNumberSequence = 1;
PhoneDialogueStart;
}
}


I haven't tested this code but I think you can get the general idea:

Each time a correct key is pressed in the correct sequence it moves the PhoneNumberSequence counter ahead by 1 (this is what the ++ means)

Once the third number is pressed correctly it resets the counter to 1 (in case repeating the same call is possible in the story) and then starts the function called "PhoneDialogueStart".

I have not included the "else" statements that deal with the numbers being wrong, but I think you probably can (and should) work those out for yourself. If not, please give another yell!

EDIT for clarity:

The problem with this code at the moment without the "else" statements is that while 234 will be detected as the correct number, 81234 will also be accepted, as will 2573904. Wrong numbers basically have no consequences and will be ignored as long as the 234 combination is pressed at some point.

Oh also: This is assuming that you only need the phone to be able to call one particular number.

Joey


Mandle

Quote from: Joey on Sun 23/02/2014 02:05:22
Thanks a lot I'll try this.

Cool...Oh one more thing: I failed to notice the need to press the "Call" button, so just make the "call" button the last key in the sequence instead of "4" being the last key as in the example.

selmiak

you should delete the sequence and the numberspushed counter when a wrong button is pushed or you can push one correct button, thousands of wrong buttons and the again a button in order and so on.

Mandle

Quote from: selmiak on Sun 23/02/2014 02:14:23
you should delete the sequence and the numberspushed counter when a wrong button is pushed or you can push one correct button, thousands of wrong buttons and the again a button in order and so on.

Cheers mate. Already explained to him about that ;)

I just think it's better for his own learning if he works out the "else" statements on his own. I told him if not to give another yell for help in here :-D

Khris

In order to check the number, compare the Textbox content to a number.
Code: ags
  if (TextBox1.Text.compareTo("5551234") == 0) Display("Calling Hollywood...");


As for how to set up dialling: GUIControls have IDs conveniently starting at 0.
If you create the buttons so that each number on the button corresponds to its ID, you can use the same function to handle each button's OnClick event.

Just add this to the GlobalScript:
Code: ags
function Dial(GUIControl *control, MouseButton button) {
  if (button != eMouseLeft) return; // only react to left clicks
  TextBox1.Text = TextBox1.Text.AppendChar(control.ID + 48);  // ascii code for 0: 48, 1: 49, etc
}

Then enter "Dial" into each number button's OnClick event field.
The button itself is passed to the function (*control), and we read its ID to determine which one was clicked.

Note that .AppendChar() returns the new String and doesn't change the supplied String. That's why your code didn't work.

mitlark

You probably would like to add a max amount of characters. Normally any TextBox limits the max amount of characters by itself, but when done by scripting (aka manually overwritting the value), it has no character limit.
Adding an if statement before appending a character probably will do the trick:
Code: ags
if (TextBox1.Text.Length < 5) TextBox1.Text.AppendChar('2'); //For a max size of 5


My two cents.

PD.: The AGS manual/helpfile is our best friend <3.
SPIN SPIN! SPIN SPIN!

Mandle

Quote from: Khris on Sun 23/02/2014 02:47:07
In order to check the number, compare the Textbox content to a number.
Code: ags
  if (TextBox1.Text.compareTo("5551234") == 0) Display("Calling Hollywood...");


As for how to set up dialling: GUIControls have IDs conveniently starting at 0.
If you create the buttons so that each number on the button corresponds to its ID, you can use the same function to handle each button's OnClick event.

Just add this to the GlobalScript:
Code: ags
function Dial(GUIControl *control, MouseButton button) {
  if (button != eMouseLeft) return; // only react to left clicks
  TextBox1.Text = TextBox1.Text.AppendChar(control.ID + 48);  // ascii code for 0: 48, 1: 49, etc
}

Then enter "Dial" into each number button's OnClick event field.
The button itself is passed to the function (*control), and we read its ID to determine which one was clicked.

Note that .AppendChar() returns the new String and doesn't change the supplied String. That's why your code didn't work.

And yeah...I guess there is always the "Khris Way" as well...

But that's only if you really need something that is elegant, works perfectly, has zero chance of screwing up anything else in your code, and is quick and easy to implement...

If that's not your thing then my way is exactly what you want!

(I realized after reading Khris' post that my way is only useful(?) if the mechanism you want is something like a bomb that explodes as soon as you cut the wrong wire in a sequence of defusing instructions. For entering a complete number and checking it after the fact my advice is pretty bloody pointless...Sorry about that...But at least I learned something from my participation in this thread eh?)(laugh)

SMF spam blocked by CleanTalk