Choosing a value without a branch
A branch for the sake of one value is unnecessary — but select is not always needed either: a comparison already has an answer
select puts one of two values into a variable: the first if the condition holds, the second
if it does not. In the block it reads as words: total = if copper >= 100 then 1 else 0.
Before picking it up it is worth understanding when it is not needed — otherwise people start writing with it what is shorter without it.
Press “step” four times.
When select is not needed
Section titled “When select is not needed”The example’s second line is op greaterThanEq enough copper 100, and enough ends up
holding a one. No choice was required: a comparison answers with a one or a zero by itself,
and that is already a finished value.
When it is the right tool
Section titled “When it is the right tool”As soon as the values stop being one and zero, a comparison stops helping.
The third line chooses between two strings: caption becomes “enough”. That cannot be had
through op at all — a comparison can only answer with a number.
The fourth line — select howMany lessThan copper 100 copper 100 — caps a number from
above: if there is less than a hundred copper, take what there is; otherwise take exactly
a hundred. howMany ends up as 100.
The general rule: select is needed when both values are arbitrary — numbers other than
zero and one, strings, items, buildings, variables.
| What is being chosen | With what |
|---|---|
| a one or a zero by a condition | op with a comparison |
| one of two ready values | select |
| one of two actions | jump: that is a branch, not a value |
| the larger or smaller of two numbers | op max and op min — shorter |
It chooses a value, not an action
Section titled “It chooses a value, not an action”Both halves of select are values that already exist: numbers, strings, variables,
objects. The instruction runs nothing and skips nothing, it simply takes one of two.
The conditions are the same as jump’s
Section titled “The conditions are the same as jump’s”There are eight of them, and the buttons carry the same signs — ==, ===, not and the rest
from the table. The words “if”, “then” and “else” in the block are
a translation; without it they are if, then, else.
Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.