Fandom

Colobot Wiki

If

67pages on
this wiki
Add New Page
Comments0 Share

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 useEdit

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.

AttentionEdit

Do not put a semicolon at the end of the line if ( ).

See alsoEdit

Programming, types and categories.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.