colobot-data/help/cbot/E/var.txt

68 lines
4.6 KiB
Plaintext
Raw Permalink Normal View History

\b;Variables
A variable is like a box, where you can put some information. The content of the variable can change during the execution of the program.
For example, you can use a variable to count the number of titanium ore units that the grabber has collected and carried to the converter. First, you must find a name for it: the name should not be too long, but explain by itself what the variable is used for. Let's call it \c;countTit\n;. This variable must contain only whole numbers, so choose the type \c;int\n;. At the beginning of the program, you must declare the variable. Then you put the value \c;0\n; into the variable, and every time you grab a titanium ore, you increase the variable by \c;1\n;. At every moment, the variable contains the number of titanium ore units that the grabber collected.
For the declaration of the variable, write the following line:
\c;\s; int countTit;\n;
To put \c;0\n; into the variable, write:
\c;\s; countTit = 0;\n;
At every titanium ore grabbed, write:
\c;\s; countTit = countTit + 1\n;
You have much freedom in the choice of the name for a variable, you can call them by any name, f. ex.: \c;dist\n;, \c;direct\n;, \c;p2\n;, \c;a\n;, \c;x\n;, \c;nothing_2_shoot_at\n;, etc.
A variable name must always begin with a letter. It can be followed by any combination of letters, digits or the underscore character \c;_\n;. You can of course not use the keywords of the CBOT language like \c;\l;for\u cbot\for;\n;, \c;\l;while\u cbot\while;\n;, \c;\l;break\u cbot\break;\n;, \c;\l;continue\u cbot\continue;\n;, \c;do\n;, etc.
You should be careful about the choice of the names that you give to variables; writing a program can become very difficult if you forget the names of variables or if you do not remember what they are used for. Elaborate your own system to find variable names, and do not use names like \c;Bmo45\n;, \c;a\n; or \c;tgBinX\n;.
In the example above, the keyword \c;int\n; indicates that this variable can contain only whole numbers, which is adequate to counting objects. If you want to measure a distance, better use a variable that can contain also real numbers, with a fractional part (like 3.45 or 0.034): in this case, use the type \c;float\n;.
In fact, a variable is made up of three parts:
1) the name,
2) the type of the information stored inside,
3) the content, i.e. the information itself.
\t;the name
You need the name to be able to distinguish the different variables from each other.
\t;the type
The \l;type\u cbot\type; determines what kind of information the variable can contain. According to the type, a variable can contain a whole number, a real number, a string, the coordinates of a point, information about an object, etc.
Here is a list of the most common variable types:
o \c;\l;int\u cbot\int;\n; for a whole number (12, -500, etc.)
o \c;\l;float\u cbot\float;\n; for a real number (3.14, 0.2, -99.98, etc.)
o \c;\l;string\u cbot\string;\n; for a character string ("Hello!", "No object found", etc.)
o \c;\l;point\u cbot\point;\n; for a x,y,z-coordinate in space
o \c;\l;object\u cbot\object;\n; for information about an object (bot, building, etc.)
\t;The content
The content of a variable is an information of the kind specified in the type. It can change during the execution of the program.
Once you declared a variable, it still does not contain any value. Before you can use it, you must put a value inside:
\c;\s; int i, j;
\s; j = 25; // j takes the value 25
\s; j = i; // can not write this, because i has got no content yet.
\n;
You can also declare a variable and put a value inside in the same line, writing:
\c;\s; int countTit = 0;
\n;
When you assign a value to a variable with the equals sign \c;=\n;, the value on the right side is copied into the value on the left side. Consider the following example:
\c;\s; int i, j;
\s; i = 5+2; // i takes the value 7
\s; j = i+6; // j takes the value of i plus 6, this is 13
\s; i = j; // i takes the value of j, this is 13
\n;
At the end of this example, both variables \c;i\n; and \c;j\n; contain the value \c;13\n;.
The following assignment is not correct, because on the left side of the equals sign \c;=\n;, there can be only a variable name:
\c;\s; i+2 = j; // impossible
\n;
If you want to exchange the values of two variables, you must use a third variable. Here is an example to exchange the content of two variables \c;a\n; and \c;b\n; :
\c;\s; int temp;
\s; temp = a; // temp takes the value of a
\s; a = b; // the content of b is copied into a
\s; b = temp; // the content of temp is copied into b
\n;
\t;See also
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.