step7: Let's use variables
The script is getting longer, isn't it? This can be complicated and difficult to read. Comments are useful in such cases. A comment is to put your favorite characters as a comment in the script. However, there are rules on how to write comments, as it should not be mistaken for an instruction. For example,
; This line is a comment
mes "message"; This part is a comment
Use like. That is
After "; (semicolon)", it becomes a comment and is ignored.
It means
. Whether you write an instruction or a sentence after the ";" has no effect on the execution of the script. So
exec "notepad"; Start Notepad
You can easily add annotations like this. This is recommended because it makes it easier to see what is happening on which line when you review the script later.
Now, let's get into the main subject, "Let's use variables." You might wonder what variables are in the first place. As it is, if you want to change the current position,
pos 150,80; Change current position to (150,80)
I specified a numerical value like this. You can replace this with a variable. Then,
x = 150; Substitute 150 for variable x
y = 80; Substitute 80 for variable y
pos x, y; change current position to (x, y)
It will be like this. The current position is still changed to (150,80). Ehhhh ?? Why eh ??
Variables are often likened to "things". It is a "thing" that remembers the numerical values. Give a name to "Iremono". For example,
"The one named x"
Of course, you can put something inside the "Iremono".
"Put the number 100 in the x"
You can put numbers inside like this. This "container" is a variable. So, the above words are
"Put the number 100 in the variable x"
Please think that. If you write this in a script,
x=100
Will be. Putting numbers in variables in this way is called "assignment". When you make an assignment, a number is stored in the variable, and after that, you can use the variable as a substitute for the number.
"Assignment" is to make a variable remember a numerical value
"Assignment" is performed by "variable name" + "= (equal)" + "numerical value"
Remember
well.
hensu
|
=
|
1234
|
|
|
|
|
|
Variable name
|
|
Number to substitute
|
Only
assignment is written in a special way unlike other instructions, but it is easy once you understand it because it is similar to an arithmetic expression. The variable name is an alphabetic name (up to 20 characters), and the number that can be remembered is an integer value such as "123" or a decimal value such as "1.5".
There are more rules for variable names. Do not use the same word as the command. You don't know if it's an instruction or a variable. Also, don't use the same name as the label described earlier.
Variables can be assigned at any time and can be changed any number of times in the script. Since one variable remembers only one number, the last assigned number will always be remembered. For example,
hen=150
cls 1
hen=50
In a script like this, the variable hen was assigned the number 150 on the first line, but the variable hen was changed to 50 on the third line. At this point, the previously assigned 150 will disappear.
But just saying it here doesn't really make sense. Therefore, let's actually check the numerical value stored in the variable. You can do that with the "mes" command that came up earlier. The "mes" command is
mes "string" + variable name
You can display the contents of the variable after the string with. You can also connect strings and variable names one after another by using the "+ (plus)" sign. Let's enter the following script using this.
hen=150
mes "The number of the current variable hen is" + hen + "."
hen=50
mes "The number of the current variable hen is" + hen + "."
stop
When I run this script ...
The number of the current variable hen is 150.
The number of the current variable hen is 50.
Is displayed. In other words, at the time of the second line, the variable hen remembered the numerical value of 150, but since there was a new assignment in the third line, the variable hen had a numerical value of 50 in the fourth line. ..
Now that we know this, let's return to the original script.
x=150
y=80
pos x,y
After all, the script
pos 150,80
It is the same as specifying. I think I'm just using names instead of numbers. You might think that. No, but it's actually very versatile and convenient. The key to that. That is the formula.
Until now, I wrote numbers such as 100 and 20 directly in the place where I put a numerical value as a parameter. And I also learned that you can write variables other than that. But, no, actually, I could have written a mathematical formula. Eh ?? This time, it is a mistake to think that there is another element ??, and it means that numbers and variables can be mixed and written according to the calculation formula. That is a mathematical formula. That is,
A mathematical formula is a numerical value and a variable, or they are connected by a calculation formula and written.
It is
. The formula is, in other words,
1+1
Or
125+200-32
And. It is a so-called arithmetic expression. So
pos 150,100
Not
pos 100+50,50*2
But it's okay. This is also the same as "150,100". No, you might wonder what "50 * 2" is ?? In HSP, the symbols used in the calculation are as follows.
"+" is addition
"-" Is subtraction
"*" Is multiplication (×)
"/" Is division (÷)
The keyboard does not have symbols such as "x" and "÷", so I use a different number instead. Therefore, "50 * 2" is 50 times 2. You can mix variables with this, so
x=30
y=100
pos x,y
mes "1. Banme"
pos x,y+50
mes "2. Banme"
Can be used like. In this example, messages are displayed at (30,100) and (30,150). Moreover, in this case, when you say "I want to move the whole thing up a little more ...", you can change the location of the two messages at once by simply changing y = 100 on the second line to y = 80, etc. reason. In this example, it's easy because there are only two messages, but if this is a message with about 10 lines, it will be very convenient compared to the trouble of changing the numerical value of the "pos" instruction one by one.
Formulas can also be used in variable assignments.
x=5
y=10
z=x*8/y
Try using it like
hen=0
hen=hen+1
Use like. In the above example, 0 is assigned to the variable hen in the first line. But what happens in the second line ?? The variable hen is 0 before it is assigned to the variable hen in the second line. The variable hen is assigned to the variable hen again by adding 1 to it, so the variable hen becomes 1 ... without having to think so complicatedly,
hen = hen + 1 adds 1 to the contents of the variable hen
All you have to do is remember
. In this way, the pattern of changing the value of the original variable by calculation is often used, so you can think of it as a cliché and remember it. Now, do you understand how to use variables in general ?? In this next part, let's explain the application further.