XScript Manual · Chapter 2

Language Basics

This chapter covers the statement format of XScript and its types, variables, constants and built-in objects. Syntax added in 4.0 is marked 4.0+; it may not work on the old engine of QMS 3.x and earlier — see Compile diagnostics — 3.x compatibility.

Statements and semicolons

In XScript a line break ends a statement. The trailing semicolon (;) may be omitted syntactically, but the official code style is to end every statement with ; and always use { } for control statements. The examples in this manual follow that style.

xscript
// Inside ( ) and [ ] you may break lines freely (4.0+)
if( IO["I_Ready"] == true &&
    IO["I_DoorClose"] == true )
{
   Log("Ready");
}
 
// A line also continues after a binary operator or ',' (4.0+)
int sum = 1 +
          2 +
          3;
 
// The trailing-backslash continuation ( old syntax ) still works
int total = 1 + \
            2;
  • Line breaks inside parentheses and after operators are 4.0+ syntax. 3.x supports only the \ continuation.
  • Several statements on one line (a = 1; b = 2;) work, but they are hard to read and produce a style warning at build time. Put them on separate lines.
  • A ; right after a control header, as in if( a );, is the dangerous C-style "empty body" notation and is reported as a warning.
  • Since 4.0, semicolons are no longer added automatically on save. The code is saved exactly as typed, and misplaced semicolons are reported as build warnings.

Comments

xscript
// line comment
/* block comment */
/*
   multi-line
   block comment
*/
 
// Inline comments naming each argument ( readability convention )
MOTOR["M_X"].MoveAbs(/*position*/100.0, /*wait*/true);

Inline comments are only a readability aid; arguments are passed by position, not by name.

When you comment out a whole block of code, matched inline /*...*/ pairs inside it are skipped line by line and only the remaining */ closes the comment, so code that contains inline parameter comments can be commented out safely.

Value types

TypeDescriptionDefault
intInteger. Computed in 64-bit internally, so arithmetic, comparison and bit operations stay exact even for large values.0
long64-bit integer. Literals beyond the int range (about ±2.1 billion) automatically become long.0
double64-bit floating point0.0
boolTrue / false. true · false or ON · OFFfalse
stringText. Immutable; supports character reads s[i] and length s.Count.""
char 4.0+A single Unicode character. Used for character comparison and character ↔ number arithmetic.'\0'
byte 4.0+Integer 0–255. Declared with brackets it becomes a byte buffer for binary communication.0
arrayA list of values. Created with a size declaration or a { } initializer.empty array
void 4.0+FUNCTION return-type declaration only — "returns nothing". Not usable as a variable type.—

string, char, array and byte buffers are covered in detail in Strings and arrays.

Declaring variables

Variables are declared as type name [= initialValue]. Several can be declared on one line separated by commas; without an initial value the variable starts at the type default. The initial value is converted to the declared type automatically.

xscript
// Declarations and initialization by type
int    count   = 0;
double offsetX = 12.5;
string lotId   = "LOT-2026-001";
bool   ready   = false;
 
// Several declarations in one line ( no initializer = type default )
int idx1, idx2, idx3;
 
// The initializer is converted to the declared type
double ratio = 1;          // stored as 1.0
int    pos   = 12.7;       // stored as 12 ( fraction truncated )
  • Module variables — variables declared in a module's Variable section are visible to every FUNCTION and STEP of the module; other modules access them as ModuleName::VariableName — see Functions — accessing other modules.
  • Local variables — variables declared inside a FUNCTION or STEP body exist only in that function. Re-declaring the same name in one function shows a warning (DUPVAR). The second declaration re-initializes the same variable, so rename it.
  • No use before declaration 4.0+ — a local variable can only be used below its declaration. Using it above the declaration is a compile error (USEBEFOREDECL), even when a module variable with the same name exists. The scope is the whole function, not the block, so a declaration in an inner block makes the name local from the start of the function.
  • Using a name that was never declared is caught at build time as an undefined symbol.
xscript
int unitIndex = 0                  // module variable ( Variable )
 
FUNCTION SelectUnit()
{
   // Log($"unit = {unitIndex}");   // ← using it here is a USEBEFOREDECL compile error
   //                                  ( the local declaration below makes it an empty local, not the module variable )
   int unitIndex = 1;
   Log($"unit = {unitIndex}");      // use it below the declaration → 1
   return true;
}

Number literals

Besides decimal, hexadecimal (0x) and binary (0b) notation and the digit separator _ are supported. 4.0+

xscript
int    dec  = 1234;             // decimal
int    hex  = 0xFF;             // hexadecimal = 255
int    bin  = 0b1010;           // binary = 10
int    mask = 0b1111_0000;      // '_' digit separator ( no effect on the value )
long   big  = 9_000_000_000;    // literals beyond the int range become long automatically
double d1   = 3.14;
double d2   = .5;               // 0.5 ( integer part may be omitted )

bool and truthiness

true · false and ON · OFF mean exactly the same thing. In machine-IO code ON · OFF is the convention.

xscript
bool run = true;
IO["O_Lamp"] = ON;          // IO output control conventionally uses ON / OFF
IO["O_Lamp"] = OFF;
 
// A number used as a condition is true when it is not 0
int remain = 3;
if( remain )
{
   Log("remain is not 0");
}

Constants

Constants of the Constant Value Editor

Constants registered in the Constant Value Editor are used by name in scripts. They are replaced by their literal value at build time, so there is no runtime cost.

xscript
// Assuming MAX_UNIT = 8 is registered in the Constant Value Editor
int i;
for(i, 0, MAX_UNIT - 1)
{
   Log($"unit {i}");
}

enum constants 4.0+

C# enum values provided by drivers can be used directly as constants named TypeName_ValueName. The code helper (auto-complete) lists the available constants.

  • Always write enum constants as TypeName_ValueName (e.g. XEventResult_No, Visibility_Collapsed). A bare value name (No, Connected …) is not treated as a constant, so a variable with such a name keeps its own value.
  • When a variable has the same name as a constant, the name reads as the variable in that scope and the build shows a warning (same name as a constant). Rename the variable to use the constant.

Built-in constants

ConstantsUsed for
EB_Ok EB_OkCancel EB_YesNo EB_YesNoCancel EB_Retry EB_Skip EB_RetrySkip EB_Reset EB_Ignore EB_Yes EB_No EB_Cancel EB_None EB_NoWaitPassButton set of message and error windows — first argument of ShowMessage · ShowError (SYS)
ER_Ok ER_Cancel ER_Yes ER_No ER_Retry ER_Skip ER_Reset ER_Ignore ER_Fail ER_TimeEnd ER_NoWaitPassButton pressed in a message window — compare with the return value of ShowMessage · ShowErrorWait
SS_Home SS_PLimit SS_NLimit SS_ZPhaseSearch signal of motor MoveSearchStop (MOTOR)
Visibility_Visible Visibility_Hidden Visibility_CollapsedControl visibility mode — GUI.SetControlVisiblity (GUI)

When a constant of the Constant Value Editor and a built-in constant share a name, the Constant Value Editor value wins.

Built-in objects and parameter prefixes

Machine and system features are reached through the built-in objects below.

FormObjectsDescription
OBJECT["name"] (indexer form)IO · CYL · MOTOR · COM · CAMERA · VISION · DEVICEAccess an individual device by its registered name (IO["O_Blow1"]). Typos in IO, motor, cylinder, communication and device names are caught as warnings at build time.
OBJECT["name"] (indexer form)DB · CONTAINER · DRIVER · LIB · CONTROL 4.0+Database · direct container access · driver-specific features (including interpolation) · project libraries · screen-control properties (CONTROL["Module::Control"])
OBJECT["name"] (indexer form)SERIES · TAG · DISPENSE · ALIGN 4.0+Global chart series · SCADA tags · dispense heads · aligns
OBJECT.member (single-object form)SYS · MATH · STR · DATE · GUI · DATA · XML · JSON · EXCELSystem · math · string · date · GUI · data helper functions
OBJECT.member (single-object form)TOWERLAMP · DEVICEMGR · SN 4.0+Tower lamp · device management · serial-number issuing
PREFIX.nameJF. · SS. · MS. · RD.JobFile · System Setup · Master Setup · RUN data parameters
xscript
IO["O_Blow1"] = ON;                        // indexer form : individual device
MOTOR["M_HeadX"].MoveAbs(100.0, true);
double s = MATH.Sin(30.0);                 // single-object form : function group
bool safety = SS.TransferXSafetyCheck;     // prefix form : parameter
  • The 3.x MOTION keyword was removed in 4.0. Interpolation is done through the motion driver object DRIVER["driver name"].
  • System functions called without a prefix, such as Log and Sleep, can also be called as SYS.Log(...) with identical behaviour. ShowError and FormatString are the exceptions — call them without SYS..

Reference by object

Identifier rules and reserved words

  • Identifiers (variable and function names) consist of letters, digits and underscore (_) and cannot start with a digit.
  • They are case sensitive (count and Count are different names).
  • Do not use the reserved words and built-in object names below as variable or function names.
CategoryReserved words
Control keywordsFUNCTION if else for while switch case default break breakall continue return goto ref
Type keywordsint long byte char double bool string array void
Literalstrue false ON OFF
Built-in objectsIO CYL CYLINDER MOTOR COM CAMERA VISION DEVICE DEVICEMGR CONTAINER TOWERLAMP DB DRIVER LIB CONTROL SN SERIES TAG DISPENSE ALIGN SYS MATH STR DATE GUI DATA XML JSON EXCEL JF SS MS RD
Subscribe