Repeat¶
Features relevant to loops
For information on how to use the Wait, SkipIteration, and StopRepeat control blocks, see Wait, Continue, and Break on the Control block page.
Repeat Loops¶
Repeat Forever loops use the repeat keyword.
Repeat Multiple loops use the repeat keyword followed by the number of times it should repeat wrapped in parentheses.
to keyword.
For Loops¶
For loops use the for keyword and the of keyword. Variables being set go before of, and the thing to iterate over comes after of.
Built-in Repeat Actions¶
For loops are used to access repeat actions like Repeat On Path and Repeat Adjacently. On the right side of the of keyword, these actions can be called as if they were normal functions.
The following actions are supported:
adjacentgridpathrangesphere
for (line l of path(damager.eyeLocation,victim.location)) {
allPlayers.displayParticleEffect(par("Flame"), l);
}
for (line i of range(5,10,2)) {
print(i); // prints 5, then 7, then 9
}
for (line l of adjacent(event.blockLocation, pattern="Cube (26 blocks)")) {
game.setBlock(item("gold_block"), l);
}
Iterating over Values¶
For loops can be used to iterate over lists and dictionaries.
The values can be inlined, or variables can be used.line data: dict[str] = {
key: "value",
apples: "oranges"
};
for (line k, line v of data) {
default.sendMessage(k, v);
}
for (line particle of [par("Flame"),par("Cloud")]) {
allPlayers.displayParticleEffect(default.location,particle);
}
The variables on the left side of of will be typed based on the type of the variable being iterated over. For this reason, it's extra important to always provide a type when declaring variables that contain lists or dictionaries.
Not specifying a list's or dict's type may cause unexpected behavior
When declaring values as a list[num], this code works as expected
When using inlined lists or dicts, it doesn't matter
If you NEED to override the type of a variable when iterating over it, cast the value on the right side of of using the as keyword.
for (line val of some_unknonwn_list as list[num]) {
// val will now have type 'num' in here,
// even if some_unknown_list is a list[any], list[str], list[txt], etc...
}
While Loops¶
While loops use the while keyword followed by a condition wrapped in parentheses.
The condition will be re-evaluated for every iteration, meaning actions and functions in the condition will be called repeatedly.
For information on how to write conditions, see Conditional Expressions.
while (num.random(1,10) != 10) {
player.sendMessage("Still going!");
wait;
}
while (!player.isStandingOnBlock(item("obsidian"))) {
player.damage(1);
wait(20);
}
Tip
When calling condition actions (e.g. player.isLookingAtBlock()), it's best to use player / entity as opposed to targeted namespaces like default or selectedEntity.
When using player or entity the compiler can put the condition inside the Repeat block itself. When you specify a target, the compiler has to generate extra code to make sure the target is properly accounted for.
Do-While loops¶
While loops evaluate their condition before the loop runs, whereas do-while loops evaluate their condition after the loop runs. This means the loop body will always run at least once.
Inverting¶
While loops and do-while loops can place a ! in front of the condition's parentheses to invert the entire condition in the same way that if statements can.