Appearance
Functions & macros
dp.fn defines a function that takes and returns scores; dp.macro takes values that a score can't hold.
ts
const clamp = dp.fn("clamp", (_ctx, v, hi) => v.min(hi));
ctx.invoke(clamp, [hp, 20], hp);| Call | Does |
|---|---|
dp.fn(name, (ctx, ...params) => result) | function with score parameters and result |
dp.macro(name, (ctx, args) => ...) | macro function with typed arguments |
ctx.invoke(fn, args, into?) | calls it; stores the result in into |
dp.createFunction(name).build(...) | a plain function with no parameters |
Return a score
The argument count is checked at build time.
ts
import { Datapack, v26_2, Selector } from "helix";
const dp = new Datapack("flow", v26_2);
const hp = dp.objective("hp").score(Selector.self());
const clamp = dp.fn("clamp", (_ctx, v, hi) => v.min(hi));
dp.createFunction("heal").build((ctx) => {
hp.add(5);
ctx.invoke(clamp, [hp, 20], hp);
});Compiled output - the real files helix emits for the code above:
txt
scoreboard players operation #zzzprivate/clamp.0 helix.var < #zzzprivate/clamp.1 helix.var
return run scoreboard players get #zzzprivate/clamp.0 helix.vartxt
scoreboard players add @s hp 5
scoreboard players operation #zzzprivate/clamp.0 helix.var = @s hp
scoreboard players set #zzzprivate/clamp.1 helix.var 20
execute store result score @s hp run function flow:zzzprivate/clamptxt
{
"pack": {
"description": "flow\nCreated using Helix",
"min_format": [
107,
1
],
"max_format": [
107,
1
]
}
}Use a value as a coordinate
Macro args are checked against the body's type. Constants pass inline; scores go through storage.
ts
const mark = dp.macro("mark", (ctx, a: { dist: number }) => {
ctx.setblock(Pos.local(0, 0, a.dist), Block.GLOWSTONE); // $setblock ^ ^ ^$(dist) ...
});
ctx.invoke(mark, { dist: range });TIP
Macro lines are re-parsed on every call. Prefer dp.fn where scores will do.