Advanced Scripting
There are many places scripting can be used in Ignition, there are Gateway and Client scripts, startup, shutdown, time scripts and more.
Script Modules
A project's Script Modules are a global library of scripts that can be called from anywhere within the scope of a project. These scripts are organized as named modules that all live under the app module. To open the Script Module Editor double-click on the Configuration > Script Modules node in the Project Browser or navigate to the Project > Script Modules menu.
Rule of Thumb: Never Copy-and-Paste a Script
If you're unsure of when to put scripts in a script module vs. embedding the script directly in an event handler, follow this simple rule. If you ever find yourself copying a script from one event handler to another, stop and refactor the script into a global script module! Then simply call your new module from the event handler. This rule will help prevent code duplication across your project, a major maintenance liability.
How to use Script Modules
To add a script module, simply right click the script library[project] package and click the New Module button. Each module is a python script that may define many functions. You may also organize modules in subpackages if you'd like. Lets suppose you added a script module named myfuncs, whose body was:
def
callMe(message):
system.gui.messageBox(message)
Now, anywhere in your project you can call:
project.myfuncs.callMe(
'Hello World'
)
To import System or not to import System
Frequently in Ignition, your scripts get system (the built-in library package in Ignition) and shared or project imported for you automatically.
Subsequent versions of Ignition beyond version 7 no longer need import system every time you create a new scope. However versions prior to version 7 still require an import system every time a system associated function is called inside a function. Ignition versions prior to version 7 still need the import system when system functions are called inside functions. Regardless, typing import system in any version of Ignition will result in the system library being available for use inside the function.
See also: Scripting Intro , Scope and Import
In this section ...