Hi,
On top of my project scripts, where global variables are defined, i have the code below.
When are global code being executed then?
Hi,
On top of my project scripts, where global variables are defined, i have the code below.
When are global code being executed then?
That's just the library where you define scripts. They do not run unless you call them. It's up to you to decide where and when.
For example you could go to the gateway events and set the scripts to run on startup or on a scheduled time. Or within a project you could have a button you click and then it runs the script.
If you haven't already the Inductive University has free modules you can watch to learn and there's a section about scripting.
Thats actually not true in this case. Because the script is not contained within a function definition, it will be excuted when the scripting engine starts. The variable will be available, but the value will be static (as in not changing).
However, @Eugene49 , this code will not be executed when the tag changes, it will run once, and isEnable will keep whatever the value of the tag is at that execution.
What are you trying to accomplish?
I figure this is the case, only run once at the start.
I have existing code that rely on global variables.
These global variables is assigned manually before project is save.
I made a perspective view, so user can set global variable from the view.
I guess the solution is, replace all the code that refer to the global variable to a tag.read command.
No, you can certainly use a script variable, you just have to take steps to initialize and update it's value.
For instance:
isEnable = False
def initialize():
isEnable = system.tag.readBlocking(["[defalut]Settings/ENABLE_SCRIPT"])[0].value
def updateIsEnable(value):
isEnable = value
system.tag.writeAsync(["[default]Settings/ENABLE_SCRIPT"]],[value])
Then in your project start up script, call the initialize() function.
Anywhere you need to read the value you can access the variable dirictly.
NewScript.isEnable
And anytime you need to update the value call updateIsEnable() with the new value.
No, those won't work. Within functions, isEnable is read-only by default, and assignments are only local to the function. See examples for the global keyword in python.
But note that proper caching is best achieved with a dictionary or similar object that can have inner elements assigned. The the initialization just creates the empty dictionary.