Functions In States, Functions In Scripts

Not all functions are permitted as regular statements in states - some may only be used within script blocks. Some are never permitted in script blocks., but may only be used in steady state. Other functions may be used anywhere. Function Usage in States, Scripts, and Threading lists where each function and statement may be used.

In order to understand why certain functions are allowed only in script blocks, it is necessary to understand the nature of a script. A script is a special group of statements that is not always active, like those in a state, but is linked to a single statement called an "action trigger" that governs its execution. (see the topic: Active States.)

When the action trigger is true, the script will execute its statements once only and in the order that they appear. While a script is executing, all other statements executing in that same thread are suspended - no other statements are allowed to run. The general rule to follow then if wondering why some functions are allowed in scripts and other are not, is to decide if the function is meant to be used continually, such as a graphic item, or whether it is meant to perform a one-time action, such as a file write. If the function must be continuously active, it must appear in the main body of the state; if it requires only a single execution from time to time, it must appear in a script. Some functions (such as addition) may be done once or continuously, and may appear anywhere.

Action statements may not appear inside a script block. Also, script blocks may not be nested inside other script blocks.