I have a script that I use a global variable in to enable logging on a clientgateway.
I thought that this should be working, but it doesn't seem to work as expected. Ignition v8.1.16:
I have this script in a "globalVars" project library package.
#global variables
logger = False
#initialize global var values
def start():
logger = True
The intention is that a startup script for the clientgateway runs globalVars.start() and toggles logger to True. However, when I print globalVars.logger in the script console I get False. Even if I try and print it after a direct call to globalVars.start()
def logTagChange2(tag, tagPath, currentVal, initialChange, clientScope=0):
if globalVars.logger:
#get values from tag
.
.
.
What am I doing wrong?
Maybe I have a scope issue in the client? I have other scripts that are dependent on this variable as well that definitely are working.
I had a lastTime prop and I was comparing it to now to figure out when the time since last log had passed the minimum sample time, but I was doing the comparison backwards. I had lastTime - now instead of now - lastTime
I really can't see any way this isn't just shadowing logger inside start. It wouldn't/shouldn't be affecting the variable from the outer scope at all (without the use of the global keyword as Kevin suggested).
For something completely different...
Could you put the initialization of logger into a function and have that function's return value act as the initialization?
That is: