Colobot Wiki
Advertisement

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 countTitanium. This variable must contain only whole numbers, so choose the type int. At the beginning of the program, you must declare the variable. Then you put the value 0 into the variable, and every time you grab a titanium ore, you increase the variable by 1. 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:

	int  countTitanium;

To put 0 into the variable, write:

	countTitanium = 0;

At every titanium ore grabbed, write:

	countTitanium = countTitanium + 1

Or:

	countTitanium += 1;

You have much freedom in the choice of the name for a variable, you can call them by any name.

Ex.: dist, direct, p2, a, x, nothing_2_shoot_at, etc.

A variable name must always begin with a letter. It can be followed by any combination of letters, digits or the underscore character (_). You can of course not use the keywords of the CBOT language like for, while, break, continue, do, 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 Bmo45 or tgBinX.

In the example above, the keyword int 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 float.

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)


The name[]

You need the name to be able to distinguish the different variables from each other.

The type[]

The 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:

  •   int for a whole number (12, -500, etc.)
  •   float for a real number (3.14, 0.2, -99.98, etc.)
  •   string for a character string ("Hello!", "No object found", etc.)
  •   point for a x,y,z-coordinate in space
  •   object for information about an object (bot, building, etc.)

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: 

	int  i, j;
	j = 25;  // j takes the value 25
	j = i;   // can not write this, because i has got no content yet.

You can also declare a variable and put a value inside in the same line, writing:

	int  countTitanium = 0;

When you assign a value to a variable with the equals sign =, the value on the right side is copied into the value on the left side. Consider the following example: 

	int  i, j;
	i = 5+2;  // i takes the value 7
	j = i+6;  // j takes the value of i plus 6, this is 13
	i = j;    // i takes the value of j, this is 13

At the end of this example, both variables i and j contain the value 13.

The following assignment is not correct, because on the left side of the equals sign =, there can be only a variable name:

	i+2 = j;  // impossible

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 a and b :

	int temp;
	temp = a;  // temp takes the value of a
	a = b;     // the content of b is copied into a
	b = temp;  // the content of temp is copied into b
Advertisement