XScript Manual · Chapter 3
Control Flow
XScript supports if, for, and while, plus switch/case/default branching.
Opening braces always go on the next line (project style rule).
if
xscript
if (IO["I_Start"] == ON)
{
Log("start pressed");
}
else if (IO["I_Reset"] == ON)
{
Log("reset pressed");
}
else
{
// idle
}Real-world pattern
xscript
// Combined sensor + flag + server-response condition
if (SYS.IsRunning && SS.UseServerRetryCount == ON
&& Common::IsMeasureUnit(ErrorUnitIndex)
&& retries < maxRetries)
{
ret = Micom::Send(ErrorUnitIndex, "send_error_reply", "reply=retry", true);
}for
XScript's for uses comma-separated arguments: for(var, start, end) or
for(var, start, end, step).
xscript
// i = 0..count-1 (ascending)
for (i, 0, count - 1)
{
moduleName = $"Module{i+1}";
totalUnitCount += JSON.GetIntValue(jsonName, $"modules[{i}].category.unitCount");
}
// Reverse iteration (step = -1)
for (i, count - 1, 0, -1)
{
DisplayList.Add(Data::ReorderNumbers[i]);
}while
Repeats while the condition is true. Common in equipment wait loops.
xscript
int startTick = SYS.TickCount;
while (UnitData::IsSending[unitIndex - 1] == OFF)
{
Sleep(100);
if (SYS.GetElasped(startTick) > 2000)
{
LogError($"{unitName} IsSending ON Failed");
ShowError(EB_Reset, 1210, $"{unitName}:IsSending ON Failed");
return false;
}
}Caution :
while(true)infinite loops are forbidden — always include a timeout and an exit condition. For simple waits, preferIO[name].Wait(true, ms)orMOTOR[name].Wait(ms).
Flow-control keywords
| Keyword | Description |
|---|---|
return | End current function/step. return false signals failure. |
break | Exit current loop |
breakall | Exit all nested loops at once |
continue | Skip current iteration |
goto "STEP_name" | Jump to another step in the sequence |
xscript
for (i, 0, SS.UnitCount - 1)
{
if (UnitData::UnitOrderNumber[i] == "")
{
continue; // skip empty units
}
if (SendUnitOrderItemStatus(i, status) == false)
{
result = false;
break; // stop on failure
}
}Tips
- Opening brace always on the next line.
- Return immediately with
return falseafterShowError(). - Split long conditions across lines; use a temp
boolif it helps readability. - Keep nesting depth ≤ 2; refactor into a function otherwise.