Alpha: this compiler is under active development and updates will break your projects. Not recommended for real use yet.
Skip to content

Loops ​

ctx.repeat runs a body a fixed number of times; ctx.while runs it while a condition holds.

ts
ctx.repeat(3, (c, i) => c.tellraw(Selector.allPlayers(), [i]));
LoopRuns
ctx.repeat(n, (ctx, i) => ...)n times, i from 0
ctx.repeat(n, body, { unroll: true })n copies in place, i a number
ctx.while(cond, body, { advance?, max? })while cond holds, at most max passes; .else after
list.forEach((ctx, el) => ...)once per element of a storage list

WARNING

A loop that stays a loop needs return run (1.20.2+), and maxCommandChainLength caps its passes per tick. Unrolled loops need neither.

When helix unrolls ​

helix writes a loop out in place when the pass count is fixed and the copies fit in 64 lines:

LoopUnrolled to
ctx.repeat(3, body)the body 3 times, i set before each copy only if the body reads it
ctx.repeat(20, longBody)20 calls to the body's function
ctx.while(cond, body, { max: 10 })10 execute if <cond> run ... copies, no counter

It stays a loop when n is a score, the body has a return, or a while has advance, .else or an or() condition. A while with no .else is one function that calls itself: the body, then execute <advance> if <cond> run return run function <self>. Turn unrolling off with optimize: { unroll: false }.

Unroll with a compile-time i ​

{ unroll: true } always writes the body out n times, and makes i a plain number, so it can go into text, positions or paths.

ts
import { Datapack, v26_2, Selector } from "helix";

const dp = new Datapack("flow", v26_2);

dp.createFunction("count").build((ctx) => {
  ctx.repeat(3, (c, i) => c.tellraw(Selector.allPlayers(), [`pass ${i}`]), { unroll: true });
});

Open in playground

Compiled output - the real files helix emits for the code above:

txt
tellraw @a {"text":"pass 0"}
tellraw @a {"text":"pass 1"}
tellraw @a {"text":"pass 2"}
txt
{
  "pack": {
    "description": "flow\nCreated using Helix",
    "min_format": [
      107,
      1
    ],
    "max_format": [
      107,
      1
    ]
  }
}

TIP

n must be a number, not a score. A return in the body ends the whole function, not the loop.

Step a ray ​

advance moves the context each pass. .else runs where the loop stopped.

ts
import { Datapack, v26_2, Detect, Pos, Block, BLOCK_TAGS } from "helix";

const dp = new Datapack("flow", v26_2);

dp.createFunction("look").build((ctx) => {
  ctx
    .while(Detect.block(Pos.here(), Block.tag(BLOCK_TAGS.AIR)), () => {}, {
      advance: (e) => e.positioned(Pos.local(0, 0, 0.5)),
    })
    .else((c) => c.say("hit"));
});

Open in playground

Compiled output - the real files helix emits for the code above:

txt
execute positioned ^ ^ ^0.5 run return run function flow:zzzprivate/look/while_0
txt
execute if block ~ ~ ~ #minecraft:air run return run function flow:zzzprivate/look/while_0/pass_0
return run say hit
txt
function flow:zzzprivate/look/while_0
txt
{
  "pack": {
    "description": "flow\nCreated using Helix",
    "min_format": [
      107,
      1
    ],
    "max_format": [
      107,
      1
    ]
  }
}

Cap a loop ​

max stops after that many passes. With no advance or .else, a small cap unrolls.

ts
import { Datapack, v26_2 } from "helix";

const dp = new Datapack("flow", v26_2);
const applied = dp.objective("solver").score("#applied");

dp.createFunction("solve").build((ctx) => {
  applied.set(1);
  ctx.while(applied.equal(1), () => applied.set(0), { max: 4 });
});

Open in playground

Compiled output - the real files helix emits for the code above:

txt
scoreboard players set #applied solver 1
execute if score #applied solver matches 1 run scoreboard players set #applied solver 0
execute if score #applied solver matches 1 run scoreboard players set #applied solver 0
execute if score #applied solver matches 1 run scoreboard players set #applied solver 0
execute if score #applied solver matches 1 run scoreboard players set #applied solver 0
txt
{
  "pack": {
    "description": "flow\nCreated using Helix",
    "min_format": [
      107,
      1
    ],
    "max_format": [
      107,
      1
    ]
  }
}

TIP

helix report flags a function that calls itself behind an if as hand-rolled-loop: write it as ctx.while or ctx.repeat so helix can pick the cheapest form.

Loop over a storage list ​

forEach walks a copy, so the body can change the list.

ts
const game = StorageData<{ players: { name: string; hp: number }[] }>(dp.storage("game"));
game.players.forEach((c, p) => c.if(p.hp.missing(), () => p.hp.set(20)));

See also ​

Released under the MIT License · Credits
These docs were written with AI; the layout and every page have been reviewed by a human.