# Global Vars Script Declaration and Assignment

**URL:** <https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [August 5, 2024, 5:29pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180 "2024-08-05T17:29:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 5, 2024, 5:29pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/1 "2024-08-05T17:29:47Z")

</div>

I have a script that I use a global variable in to enable logging on a ~~client~~ _gateway_.

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.

```python
#global variables

logger = False

#initialize global var values
def start():
	logger = True

```

The intention is that a startup script for the ~~client~~ _gateway_ 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()`

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

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 5, 2024, 5:38pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/2 "2024-08-05T17:38:06Z")

</div>

What causes `logTagChange2` to be called?

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 5, 2024, 5:38pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/3 "2024-08-05T17:38:16Z")

</div>

tag change script(s)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 5, 2024, 5:39pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/4 "2024-08-05T17:39:21Z")

</div>

A tag change script runs in the Gateway.

A Client Startup script runs in the Client.

Global state/variables are not shared between these 2 entirely separate contexts.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 5, 2024, 5:40pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/5 "2024-08-05T17:40:04Z")

</div>

Sorry, yes I misspoke.` globalVars.start()` is run on a **gateway** startup script, not client.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 5, 2024, 5:45pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/6 "2024-08-05T17:45:00Z")

</div>

> [@amarks](#):
>
> ```python
> #global variables
> 
> logger = False
> 
> #initialize global var values
> def start():
> logger = True
> 
> ```

I think this needs to be:

```python
#initialize global var values
def start():
    global logger
    logger = True

```

but also I think there's some standard advice about using `system.util.globals` instead

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 5, 2024, 5:55pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/7 "2024-08-05T17:55:41Z")

</div>

I found the actual issue in my script.

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`

Thanks for the help

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 5, 2024, 6:31pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/8 "2024-08-05T18:31:57Z")

</div>

> [@amarks](#):
>
> ```python
> #global variables
> 
> logger = False
> 
> #initialize global var values
> def start():
> logger = True
> 
> ```

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:

```python
logger = __getInitialState()

def __getInitialState():
    if someComplicatedCondition:
        return True
    else:
        return False

```

That seems easier to reason about (at least, in this isolated, cut down example you provided) than the current order of operations.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 5, 2024, 7:06pm UTC](https://forum.inductiveautomation.com/t/global-vars-script-declaration-and-assignment/94180/9 "2024-08-05T19:06:20Z")

</div>

Thanks for the input. I'll take a look at other options for the same functionality.
