This is an old revision of the document!
Table of Contents
Debugging complex scripts
Project initialization
Non - volatile registers
Please keep in mind that registers of Dxx type and some other internal registers, except for DSxx, Sxx return to the initial state (usually 0) after initialization of the project (what happens with any editing of project elements - scripts, registers, connections, etc.). If such registers are used as variables to generate event triggers or conditions for executing other scripts, this can lead to a violation of the execution logic. You should consider this feature when choosing registers as input and output script variables.
Global variables
Variables in scripts declared before the 'main' function store their values between script calls, but also reset when initializing. They can be used to store constants, coefficients, and other similar values.
First scan
Some script can be assigned to run on the first scan when the project is initialized and make some initialization of variables from one place. Sometimes you might need to define inside the script itself whether this was the first call after initialization. You can create a global variable first_scan:
first_scan = true -- global var remember value between scans function main (userId) local var = 0 -- local var will be initialized in each script call if first_scan then first_scan = false -[[ Actions for the first scan --]] end --if first_scan -- other actions end -- main
Remember that registers such as Dxx, CDxx and other internal registers, except for DSxx, return to the initial state (usually 0) after initializing the project (which happens when editing project elements - scripts, registers, connections, etc.). If such registers are used as variables to generate event triggers or conditions for executing other scripts, this can lead to a violation of the execution logic. You should consider this feature when choosing registers as input and output script variables.