I have a few questions about the finer details of how script modules work.
Part 1 ~
In a script module (shared.testing) I have the following:
x = 1
print 'before x=', x
def changeX():
global x
print 'before:', x,
x += 1
system.tag.write('x', x)
print 'after:', x
print 'after x=', x
the first time I run the script I get the following output:
>>> shared.testing.changeX()
before x= 1
after x= 1
before: 1 after: 2
the second time I run the script I get the following output:
>>> shared.testing.changeX()
before: 2 after: 3
what is different about the first time I call a function from a script module than the other times? Why do the print statements get executed the first time and not any time after?
Part 2 ~
Condidering the same script above. If I call the script function changeX() from a button action performed event script on a window in the designer in preview mode, say 4 times (assuming right after the script was commited) then the output on the console window is:
before x= 1
after x= 1
before: 1 after: 2
before: 2 after: 3
before: 3 after: 4
before: 4 after: 5
now if I call the script function from the script console I get:
>>> shared.testing.changeX()
before x= 1
after x= 1
before: 1 after: 2
It seems the designer and script console each have their own instance of the global variable ‘x’. Does the designer have a different scope than the script console? I thought (at least with previous versions of Ignition) that the designer and script console had the same scope (I recently upgraded from 7.7.7 to 7.9.3)? It also seems that each client instance and the gateway all have their own scope for these scripts and thier variables. But that’s expected I suppose.
Part 3 ~
The first time I call the changeX() function the global variable ‘x’ is instantiated. Say I do this from an event script on a window. If I close the window and open it again the global variable persists. When does it go away? When the client is closed? What about in other scopes (ie. gateway and script console)?