XScript Manual · Chapter 50

title: "SYS — system globals & utilities" chapter: 50

SYS — system global object

SYS bundles system-level utilities — time, files, HTTP, module execution. It is the single most-used global and unifies OS access for scripts.

Time / ticks

Property / MethodTypeDescription
SYS.TickCountintElapsed ms since startup
SYS.GetTickCount()intSame, as function
SYS.GetElasped(int startTick)intTickCount - startTick
SYS.SecondCountintSeconds counter
SYS.Year / Month / Day / HourintCurrent date/time parts
SYS.DateStringstringYYYYMMDD
SYS.TimeStringstringHHMMSS
SYS.DateTimeStringstringYYYYMMDD_HHMMSS
SYS.DateTimeFilenamestringFile-safe timestamp
SYS.DateTimeMsecStringstringWith milliseconds
SYS.CurrentDateTimeInISO8601stringISO 8601
SYS.GetDateTimeStringFormat(string format)stringC# format string
int start = SYS.TickCount;
DoSomething();
int elapsed = SYS.GetElasped(start);
Log($"Elapsed: {elapsed} ms");

Files / paths

MethodDescription
bool ExistsFile(string path)File exists
string ReadAllText(string path)Read whole file
bool WriteAllText(string path, string data)Write whole file
array ReadAllLines(string path)Read as lines
bool WriteAllLines(string path, array lines)Write as lines
bool AppendLine(string path, string data)Append line
bool CreateDirectory(string path)mkdir -p
bool DeleteFile(string path)Delete
bool CopyFile(string src, string dest, bool overwrite)Copy
string Combine(string path1, string path2)Join path
string GetFileName(string path)Base name
string GetFileNameWithoutExtension(string path)No extension
string GetValidFileName(string name)Sanitize invalid chars
string ProjectBaseDirectoryProject root
string DownloadsFolderPathDownloads folder
string file = SYS.Combine(SYS.ProjectBaseDirectory, "JSON\\cube_machine.json");
if (SYS.ExistsFile(file) == false)
{
    LogError($"Not found: {file}");
    return false;
}
string text = SYS.ReadAllText(file);

HTTP

MethodDescription
string SendHttpGet(string url) / with tokenGET
string SendHttpPost(string url, string data) / with tokenPOST
string SendHttpDelete(string url, string token)DELETE
string SendHttpPatch(string url, string data, string token)PATCH
SYS.HTTP_ERROR_STRSentinel for error
SYS.LastHttpErrorLast error message
string url = SS.MachineApiServer + "/machine-status";
string result = SYS.SendHttpPost(url, data, SS.HttpToken);
if (result == SYS.HTTP_ERROR_STR)
{
    LogError($"ERROR: {SYS.LastHttpError}");
    return false;
}

Parameters / module execution

MethodDescription
bool SaveSetupParam(void)Persist setup params
bool LoadJobFileParam(void)Reload job-file params
bool GetParamValue(string name, ref string value)Read parameter
bool SetModuleVar(string name, value) / bool GetModuleVar(string name, ref value)Cross-module vars
bool RunScriptFunction(string module, string func, args...)Call cross-module
bool StartModule(string module)Restart module
void StopMotorAll(void)Stop all axes
bool CheckMotorName(string name)Axis exists
if (SYS.RunScriptFunction(moduleName, "ParseJsonToModuleData", data) == false)
{
    ShowError(EB_Ok, 107, moduleName);
    return false;
}

System state / errors

PropertyTypeDescription
SYS.IsRunningboolAuto-run active
SYS.ManualThreadExitSignalboolManual thread exit requested
SYS.LastErrorCodeintLast error code
SYS.LastErrorMsgstringLast error message
SYS.LastErrorModuleNamestringOrigin module
SYS.ProjectVersion / SYS.QMSVersionstringVersions
if (SYS.IsRunning == false)
{
    return true;
}
 
SYS.LastErrorCode = 0;
SYS.LastErrorMsg = "";
SYS.ResetError();

Real-world — HTTP with timing & logging

FUNCTION GetServerOrderCount()
{
    string url = $"{SS.HttpOrderAddr}/order-queue/machines/{SS.MachineId}/status";
    int s = SYS.TickCount;
    string data = SYS.SendHttpGet(url, SS.HttpToken);
 
    if (data == SYS.HTTP_ERROR_STR)
    {
        if (SS.OrderMode == 1)
        {
            LogError($"GetServerOrderCount Error :[{SYS.TickCount-s}ms] {SYS.LastHttpError}");
        }
        return -1;
    }
 
    int count = Order::GetOrderCount(data);
    return count;
}

Tips

  • Long waits: measure with SYS.GetElasped(startTick) and check ManualThreadExitSignal between sleeps.
  • File IO: always precede writes with CreateDirectory and ExistsFile for reads.
  • HTTP calls don't throw — compare result with SYS.HTTP_ERROR_STR.
  • Cross-module calls: always go through SetModuleVar / GetModuleVar / RunScriptFunction.