The script should look like this when it's done:
The double equals compares for equality, so this if statement's condition will only be true if strMsg contains "Hello World!" with the exact same capitalization and spelling.
Tip: An easy way to add an if statement on the script editor is to highlight the line of text that you want to put "inside" the if, click the Add Code button, and then choose Quick Scripts > "if statement" from the menu. An if statement "template" will be added around the line that you highlighted. You can then edit the template to fit your needs.
2 ) P re ss F 5 t o p rev i e w t h e p r oj ec t . Wh e n t h e p rev i e w op e n s , c li ck on t h e bu tt on t o t r igg er t h e s cr ip t .
This time, nothing happens, because strMsg is still set to "Good Morning!" in the On Preload event. "Good Morning!" doesn't equal "Hello World!" so the if condition fails, and the block of code between the "then" and "end" keywords is skipped entirely.
3 ) E x i t f r o m t h e p rev i e w. E di t t h e bu tt on obj ec t ' s On C li ck s cr ip t t o c h a ng e t h e == t o ~= , li ke t hi s :
i f s t r M s g ~= " He llo Wo r ld !" t h e n
D i a log. M e ss a g e ("E very d ay I w ake up a nd s ay ... " , s t r M s g ) ; e nd
The script should look like this when it's done:
The tilde equals (~=) compares for inequality, so this if statement's condition will only be true if strMsg contains anything but "Hello World!" with the exact same capitalization and spelling.
4 ) P rev i e w t h e p r oj ec t . Wh e n t h e p rev i e w op e n s , c li ck on t h e bu tt on.
This time, because strMsg contains "Good Morning!", which is definitely not equal to "Hello World!", the message will appear.
The == and ~= operators are fine when you want to check strings for an exact match. But what if you're not sure of the capitalization? What if the variable contains a string that the user typed in, and you don't care if they capitalized everything correctly?
One solution is to use an old programmer's trick: just convert the contents of the unknown string to all lowercase (or all uppercase), and do the same to the string you want to match.
Let's modify our script to ask the user for a message, and then display what they typed, but only if they typed the words "hello world" followed by an exclamation mark.
5 ) E x i t f r o m t h e p rev i e w. E di t t h e bu tt on obj ec t ' s On C li ck s cr ip t t o loo k li ke t hi s :
s t r M s g = D i a log. I npu t("" , " E n t er y ou r me ss a g e :" ) ; i f St r ing. U pp er ( s t r M s g ) == " H E LLO WO R L D !" t h e n
D i a log. M e ss a g e ("E very d ay I w ake up a nd s ay ... " , s t r M s g ) ;
e l s e e nd
D i a log. M e ss a g e (" Um ... " , " Y ou didn ' t t y p e He llo Wo r ld !" ) ;
The first line uses a Dialog.Input action to pop up a message dialog with an input field that the user can type into. Whatever the user types is then assigned to the strMsg variable.
Note: This new value replaces the value that was assigned to strMsg in the page's On Preload event.