# Declare Variables

**URL:** <https://forum.inductiveautomation.com/t/declare-variables/75149>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [June 16, 2023, 2:58pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149 "2023-06-16T14:58:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_Vilela](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thiago_vilela/32/37352_2.png) [@Thiago\_Vilela](https://forum.inductiveautomation.com/u/Thiago_Vilela)\
**Post date:** [June 16, 2023, 2:58pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/1 "2023-06-16T14:58:08Z")

</div>

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)

```auto
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**

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 16, 2023, 4:29pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/2 "2023-06-16T16:29:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 16, 2023, 4:34pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/3 "2023-06-16T16:34:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 16, 2023, 5:10pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/4 "2023-06-16T17:10:44Z")

</div>

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. 😁

---

<div class="post-metadata">

**Author:** ![Thiago\_Vilela](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thiago_vilela/32/37352_2.png) [@Thiago\_Vilela](https://forum.inductiveautomation.com/u/Thiago_Vilela)\
**Post date:** [June 16, 2023, 5:19pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/5 "2023-06-16T17:19:28Z")

</div>

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**.

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 16, 2023, 5:27pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/6 "2023-06-16T17:27:35Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Thiago\_Vilela](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thiago_vilela/32/37352_2.png) [@Thiago\_Vilela](https://forum.inductiveautomation.com/u/Thiago_Vilela)\
**Post date:** [June 16, 2023, 5:40pm UTC](https://forum.inductiveautomation.com/t/declare-variables/75149/7 "2023-06-16T17:40:14Z")

</div>

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