With the instruction if() {} you can execute a set of instructions only if a certain condition is true. Write the condition in brackets (), and the instructions in braces {}.
Basic use[]
Here is a concrete example: The bot will shoot only if the target is closer than 40 meters:
item = radar(AlienAnt); if (distance(position, item.position) < 40) { fire(1); }
You can also test if an object exists at all. If the instruction radar(); does not find the requested object, it returns the value null. So you can test if an object does not exists with the condition (item == null), or test if it exists with (item != null). Two equal signs == test equality, an exclamation mark followed by an equal sign != test inequality. Here is a test that will go to rechage the power cell only if there is a power station:
item = radar(PowerStation); if (item != null) { goto(item.position); wait(5); }
For specialists Syntax:
if ( condition ) {
Instructions A ...
} else {
Instructions B ...
}
With this conditional structure you can execute a bloc A or a bloc B depending on a condition. If the condition is true, bloc A is executed. If the condition is false, bloc B is executed. Part else { } is not compulsory.
Attention[]
Do not put a semicolon at the end of the line if ( ).
See also[]
Programming, types and categories.