step5: Let's flag the script
You know that a script is a collection of instructions and is executed in order from the first line. If you execute all the lines, it may stop as it is. Now let's not only learn the instructions that control the flow of the program. It seems difficult to write like this.
Now let's flag the script. Eh ?? What is a flag ?? That is to put a mark on a specific line in the script. When a mountaineer climbs a mountain, he stands up. For example, this script,
*hata1
|
|
|
cls
|
*hata2
|
|
|
mes "Yes, hello"
|
*hata3
|
|
|
mes "This is a test to raise a flag"
|
There are three flags on it. Yes, the lines "* hata1", "* hata2", and "* hata3". By entering the name in English letters after the "* (asterisk)", you can put your favorite name anywhere you like and raise the flag. The promise here is
Do not put TAB first, unlike the - instruction
Name after
- * with alphabetic characters (up to 59 characters)
- The same name must not exist more than once
However, I think the question naturally arises, is there anything just because I had a hard time raising the flag? If you run the script in the above example with F5,
Yes, hello
It's a test to raise a flag
As you have learned so far, you will only get the result of executing the instructions in order from the first line. It seems that the flag that was set up is also ignored. Now let's modify the script as follows:
*hata1
|
|
|
cls
goto *hata3
|
*hata2
|
|
|
mes "Yes, hello"
|
*hata3
|
|
|
mes "This is a test to raise a flag"
|
This time, the mysterious command "go to * hata3" came out on the third line. For the time being, let's execute it without thinking about anything.
This is a test to raise a flag
Only the line that says is coming out. This is because the command "goto" changed the flow of the script. In other words, the "goto" instruction has the function of changing the execution flow to the flag with the name specified by the parameter. For the first time, the flags that have been raised will be useful. The instruction executed from the 1st line skips the 4th and 5th lines by the "goto" instruction on the 3rd line, suddenly jumps to the 6th line with "* hata3", and is executed again from there. I went.
The purpose of raising the flag in this way is to specify the location when you want to take the script flow to another location. And this is officially called "label" in HSP. Let's stop the name of these flags and call them labels. It's cool. That's why the 6th line is a label named "* hata3". The "goto" instruction jumps to the specified label.
And with this label, the application is even wider. Now let's enter a new script like this:
|
cls
mes "Press the button"
button "PUSH",*hata1
button "BYE",*hata2
stop
|
*hata1
|
|
|
mes "Oh, I pushed it"
stop
|
*hata2
|
|
|
end
|
You've come up with new instructions, stop and end. The stop instruction stops execution on the spot, and the end instruction closes the window and terminates the program itself. Keep in mind that it is an important instruction to control the flow together with the goto instruction.
Statement
|
Parameters
|
Meaning
|
stop
|
None
|
Stop execution
|
end
|
None
|
End execution
|
goto
|
Label
|
Jump to the specified label
|
button
|
Strings, labels
|
Display button, jump to specified label when pressed
|
Then let's execute it immediately. Now, the button has finally appeared. If you click on the button that says "PUSH", you should see the message "Oh, you pressed it." Then, when you click the "BYE" button, the window closes and ends.
The "button" instruction that first appeared here has the following functions.
- Show pushbutton
Display the specified character on the
- button
Jump to the specified label when the
- button is pressed
This "button" instruction has two parameters unlike the previous instructions. In the case of the third line, there is a part called "" PUSH "" and a part called "* hata1". Even if there are multiple parameters attached to the instruction like this, it will come out more and more from now on. At that time, it is a promise to put ", (comma)" between the parameters. So
button
|
"PUSH",
|
*hata1
|
|
|
|
|
|
|
Instruction
|
Parameter 1
|
Parameter 2
|
It will be separated by
. The "button" command specifies the character string to be displayed on the button in parameter 1 and the label to jump when pressed in parameter 2.
What happens when you press the "BYE" button? It jumps to the label "* hata2", and beyond that there is an "end" instruction. This command is very easy. As the name implies, it is an instruction that ends the script. When the "end" instruction is executed, the window is closed and the application itself is terminated. It is the same as selecting alt + F4 or the "Close" menu.
By the way, this will further expand the range of applications. By pressing a button, another image is displayed, another message is displayed, and so on, there is a great possibility. Let's review here a little, and if you remember it properly, let's move on to the next.