Using lambdas in Gateway event scripts

I have a portion of code I’m running in a gateway event script (trimmed out for clarity):

if initialChange:
	from sys import exit
	exit(0)
tagPath = 'Daicel/DSSA_AZ/NB/A5/SN_Req/'
#Get the variables. Use a readAll for efficiency
tags = ['Year', 'Type', 'SN']
vals = map(lambda x: tagPath + x, tags)
logger.debug("vals: %s" % vals)

but I recieve this exception when it runs:

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 16, in File "", line 16, in NameError: global name 'tagPath' is not defined

However; this block of code runs without exception:

if initialChange:
	from sys import exit
	exit(0)
tagPath = 'Daicel/DSSA_AZ/NB/A5/SN_Req/'
#Get the variables. Use a readAll for efficiency
tags = ['Year', 'Type', 'SN']
vals = map(lambda x: 'Daicel/DSSA_AZ/NB/A5/SN_Req/' + x, tags)
logger.debug("vals: %s" % vals)

Also If I run the code in the script console, with the reference to tagPath, I don’t have a problem either.
I don’t really understand why the gateway event script has a problem with the first block of code here…

Gateway event scripts use legacy python scoping rules, presumably for backward compatibility reasons. The quickest fix is to move your code into a function in a project script module and call that function (with needed event local variables as arguments). The project script module will run with modern python scoping.

1 Like

That would explain it, I have done this before, but I was calling a project scoped script from the gateway event. Thanks!