Control flow
Conditionals
if (confidence > 0.5) {
x = detection.x;
} else {
x = 0;
}
else if chains:
if (distance < 30) {
speed = 1.0;
} else if (distance < 100) {
speed = 0.6;
} else {
speed = 0.3;
}
Loops
for over an array:
for (kp in detection.keypoints) {
if (kp.confidence > 0.5) {
drawCircle(kp.x, kp.y, 3, "red");
}
}
while:
while (i < 10) {
state.history = append(state.history, i);
i = i + 1;
}
Existence check
Use isset() to test whether a variable has been initialized — useful for one-time state setup:
if (!isset(state.startTime)) {
state.startTime = time;
}
Conditional output
Inside any function or block, return exits early:
if (!isActive()) {
return (0, 0);
}