Declare Variables

Hello everyone.

I would like to know if it is possible to record values in variables. I would like to declare at the beginning of the program and use it in the other parts of the script for example:

PrinterNameTest will come from the SQL database using runNamedQuery when the program is opened(this part is okay)

def Variables(self):
	vPrinterName = PrinterNameTest

def ShowsPrinterName(self):
	SystemTest.Variables(self)

	view = self.view
	vPath = 'root/FlexTitle/'

	view.getChild(vPath + "lbProgTitleName").custom.PrinterName= vPrinterName 

The problem in the above code is that I can't use the variable throughout the other parts of the script other than calling it every time, instead use only vPrinterName

There are multiple ways to store data in ignition. Usually not through scripting, though possible.

In your case, it looks like you're looking for session custom properties.

1 Like

Just to expand on that because I think this is something that confuses a lot of people -

if a variable is shared among all users/the GW and it is calculated in some fashion (value from DB, or OPC tag, or expression on the current time) - use a Gateway Tag

If the variable is specific to the current client (vision or perspective client)and is calculated in some way - use a client tag or custom session property

If the variable is just specific to a project library and is a constant - declare it in a project script library at the top like PRINTER_NAME = 'someName' and reference it via myLibrary.PRINTER_NAME.

You can also set top level variables of scripts to do more a la @pturmel's suggestion elsewhere on this forum, but personally I find for most use cases the above as I described is sufficient.

1 Like

If just within a script, use a top level global as Brian suggests. You can write to such a global from within a function by the using python/jython's -- wait for it -- global statement. :grin:

3 Likes

Thanks all for the return.

The situation here is (Perspective Designer):
I have returned data from SQL and I stored it in custom property like:

I have a script in Project Liberty named ProjTest.

view.getChild(vPath + "lbProgTitleName").custom.PrinterName

In this script, I have a lot of scripts, and I would like to declare the variable to use in another def in the same script.

def Variables(self):
	global vPrinterName 

	view = self.view
	vPath = 'root/FlexTitle/'

	vPrinterName = view.getChild(vPath + "lbProgTitleName").custom.PrinterName
	x=2
	y=3

def ShowsPrinterName(self):
	view = self.view
	vPath = 'root/FlexBody/'

	view.getChild(vPath + "XPTO").props.text = vPrinterName 

How I can get this variable vPrinterName declared before and use it in other parts of my code?

Assign something as a default at the top level (outside any def).

vPrinterName = 'Default'

def Variables(self):
	global vPrinterName 

	view = self.view
	vPath = 'root/FlexTitle/'

	vPrinterName = view.getChild(vPath + "lbProgTitleName").custom.PrinterName
	x=2
	y=3

If you need x and y to propagate to other functions, do the same with them.

1 Like

Awesome @pturmel, thank you so much.
I Add the "Default" to my script and works perfectly.