Appearance
Math and /compute
math`...` writes a formula over scores and compiles it to one /compute on 26.3+, or a chain of scoreboard operations below that.
ts
math`-${dot} + min((${distSq} - ${ropeLenSq}) / ${DIV}, ${MAX})`.into(coef);${} holes take a Score, ScoreVec3, number, another math expression, or a ContextInt/ContextFloat value. .into(dest) emits it.
| Ops | Versions |
|---|---|
+ - * / %, unary -, min max abs dot/· len2 vec | all |
sqrt sin cos pow avg round floor ceil len, 0.5 literals | 26.3+ |
ContextInt/ContextFloat holes (uniform, storage, conditional...) | 26.3+ |
Using a 26.3+ op on an older target fails the build, naming the op and version.
Write a formula
ts
import { Datapack, v26_2, ScoreTarget, math } from "helix";
const dp = new Datapack("pythagoras-demo", v26_2);
const stats = dp.objective("stats");
const tick = dp.createFunction("tick");
tick.build((ctx) => {
const a = stats.score(ScoreTarget("a"));
const b = stats.score(ScoreTarget("b"));
math`${a} * ${a} + ${b} * ${b}`.into(stats.score(ScoreTarget("hyp_sq")));
});Compiled output - the real files helix emits for the code above:
txt
scoreboard players operation hyp_sq stats = a stats
scoreboard players operation hyp_sq stats *= a stats
scoreboard players operation #_t0 stats = b stats
scoreboard players operation #_t0 stats *= b stats
scoreboard players operation hyp_sq stats += #_t0 statsjson
{
"values": [
"pythagoras-demo:tick"
]
}txt
{
"pack": {
"description": "pythagoras-demo\nCreated using Helix",
"min_format": [
107,
1
],
"max_format": [
107,
1
]
}
}WARNING
Integer / floors toward -infinity and % is floor-modulo, like the scoreboard. In a float formula % truncates instead, so -5 % 2 is -1.
Get decimals
Float ops and fractional literals make the formula float. It truncates once, when it lands in the score, so scale inside the formula to keep decimals.
ts
import { Datapack, v26_3, ScoreTarget, ScoreVec3, math } from "helix";
const dp = new Datapack("sqrt-demo", v26_3);
const phys = dp.objective("phys");
const cell = (name) => phys.score(ScoreTarget(name));
const tick = dp.createFunction("tick");
tick.build((ctx) => {
const vel = new ScoreVec3(cell("vx"), cell("vy"), cell("vz"));
math`round(len(${vel}))`.into(cell("speed"));
math`len(${vel}) * 100`.into(cell("speed_centi")); // two decimal places
});Compiled output - the real files helix emits for the code above:
txt
execute store result score speed phys run compute default integer {"type":"from_float","input":{"type":"round","input":{"type":"length","inputs":["sqrt-demo:zzz/0","sqrt-demo:zzz/1","sqrt-demo:zzz/2"]}}}
execute store result score speed_centi phys run compute default integer {"type":"from_float","input":{"type":"mul","inputs":[{"type":"length","inputs":["sqrt-demo:zzz/0","sqrt-demo:zzz/1","sqrt-demo:zzz/2"]},100]}}json
{
"values": [
"sqrt-demo:tick"
]
}json
{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vx"},"score":"phys"}}json
{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vy"},"score":"phys"}}json
{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vz"},"score":"phys"}}txt
{
"pack": {
"description": "sqrt-demo\nCreated using Helix",
"min_format": [
121,
0
],
"max_format": [
121,
0
]
}
}TIP
${a} / 2 floors; ${a} / 2.0 divides for real. len(a, b) takes scalar legs too.
Work with vectors
ScoreVec3 holes apply per axis. dot/· and len2 give a scalar; vec(x, y, z) builds a vector.
ts
import { Datapack, v26_2, ScoreTarget, ScoreVec3, math } from "helix";
const dp = new Datapack("math-vec-demo", v26_2);
const phys = dp.objective("phys");
const cell = (name) => phys.score(ScoreTarget(name));
const tick = dp.createFunction("tick");
tick.build((ctx) => {
const vel = new ScoreVec3(cell("vx"), cell("vy"), cell("vz"));
const gravity = cell("gravity");
math`vec(${vel.x}, ${vel.y} + ${gravity}, ${vel.z})`.into(vel);
math`len2(${vel})`.into(cell("speedSq"));
});Compiled output - the real files helix emits for the code above:
txt
scoreboard players operation vy phys += gravity phys
scoreboard players operation speedSq phys = vx phys
scoreboard players operation speedSq phys *= vx phys
scoreboard players operation #_t0 phys = vy phys
scoreboard players operation #_t0 phys *= vy phys
scoreboard players operation speedSq phys += #_t0 phys
scoreboard players operation #_t0 phys = vz phys
scoreboard players operation #_t0 phys *= vz phys
scoreboard players operation speedSq phys += #_t0 physjson
{
"values": [
"math-vec-demo:tick"
]
}txt
{
"pack": {
"description": "math-vec-demo\nCreated using Helix",
"min_format": [
107,
1
],
"max_format": [
107,
1
]
}
}Use random, storage or conditional values
ContextInt and ContextFloat build values a formula can't spell. Drop them in as holes; it's still one command.
ts
import { Datapack, v26_3, ScoreTarget, Selector, ContextFloat, math } from "helix";
const dp = new Datapack("ballistics-demo", v26_3);
const shot = dp.objective("shot");
const tick = dp.createFunction("tick");
tick.build((ctx) => {
const t = shot.score(Selector.self());
const spread = ContextFloat.uniform(-0.01, 0.01);
math`(0.5 * 0.05 * pow(${t}, 2) + ${spread}) * 1000`.into(
shot.score(ScoreTarget("drop")),
);
});Compiled output - the real files helix emits for the code above:
txt
execute store result score drop shot run compute default integer {"type":"from_float","input":{"type":"mul","inputs":[{"type":"add","inputs":[{"type":"mul","inputs":[0.5,0.05,{"type":"from_int","input":{"type":"pow","base":{"type":"score","target":{"type":"context","target":"this"},"score":"shot"},"exponent":2}}]},{"type":"uniform","min":-0.01,"max":0.01}]},1000]}}json
{
"values": [
"ballistics-demo:tick"
]
}txt
{
"pack": {
"description": "ballistics-demo\nCreated using Helix",
"min_format": [
121,
0
],
"max_format": [
121,
0
]
}
}Write into entity or block data
.into() only takes scores. For other targets, pass .provider (int) or .floatProvider (float) to ctx.compute().
ts
import { Datapack, v26_3, ScoreTarget, ScoreVec3, Selector, math } from "helix";
const dp = new Datapack("compute-dest-demo", v26_3);
const phys = dp.objective("phys");
const cell = (name) => phys.score(ScoreTarget(name));
const tick = dp.createFunction("tick");
tick.build((ctx) => {
const vel = new ScoreVec3(cell("vx"), cell("vy"), cell("vz"));
ctx.compute().entityFloat(Selector.self(), math`len(${vel}) * 0.05`.floatProvider);
});Compiled output - the real files helix emits for the code above:
txt
compute entity @s float {"type":"mul","inputs":[{"type":"length","inputs":[{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vx"},"score":"phys"}},{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vy"},"score":"phys"}},{"type":"from_int","input":{"type":"score","target":{"type":"fixed","name":"vz"},"score":"phys"}}]},0.05]}json
{
"values": [
"compute-dest-demo:tick"
]
}txt
{
"pack": {
"description": "compute-dest-demo\nCreated using Helix",
"min_format": [
121,
0
],
"max_format": [
121,
0
]
}
}ctx.compute() | Writes to |
|---|---|
defaultFloat(p, scale?) / defaultInteger(p) | the context's objective |
entityFloat(t, p, scale?) / entityInteger(t, p) | entity data |
blockFloat(pos, p, scale?) / blockInteger(pos, p) | block data |
ctx.compute() is 26.3+ only, and both provider getters are scalar-only.