# \[SOLVED\] How do global variables work in scripting?

**URL:** <https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484>\
**Category:** General Discussion\
**Created:** [September 8, 2020, 6:16pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484 "2020-09-08T18:16:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [September 8, 2020, 6:16pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/1 "2020-09-08T18:16:27Z")

</div>

Good afternoon,

I have the following code running from a pushbutton on my Vision client:

```
x = system.tag.read('[client]HMI/x').value

def myFunc():
    y = x
    if y == 0:
        return 0
    else:
        return y

```

Sometimes when I press the pushbutton I get the correct value of x and other times I get zero. Does anyone know why?

---

<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:** [September 8, 2020, 6:36pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/2 "2020-09-08T18:36:33Z")

</div>

Where is the above code? In the button? Or in a script module? If the latter, the assignment to `x` will only happen once, when the script module loads.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [September 8, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/3 "2020-09-08T18:41:38Z")

</div>

It’s in a script module. Doesn’t the script module load again each time I call a function from a script? For example if I had two buttons calling buttonA and buttonB wouldn’t I get 5 twice? Or would I get an error since x isn’t defined in buttonB().

```
x = system.tag.read('[client]HMI/x').value ## THIS EQUALS 5

def buttonA():
    y = x
    if y == 0:
        return 0
    else:
        return y

def buttonB():
    y = x
    if y == 0:
        return 0
    else:
        return y
```

---

<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:** [September 8, 2020, 6:43pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/4 "2020-09-08T18:43:30Z")

</div>

> [@bmeyers](#):
>
> Doesn’t the script module load again each time I call a function from a script?

No. That's the point of a script module--loads once. Running the "def" statement creates the function as a named object, nicely not-quite-compiled, so it runs fast when you call it.

---

<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:** [September 8, 2020, 6:45pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/5 "2020-09-08T18:45:52Z")

</div>

Consider reading up on python’s import statement and how imported modules are run just once upon load.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [September 9, 2020, 1:03pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/6 "2020-09-09T13:03:30Z")

</div>

The difference with Ignition is I never have to import anything to run the script. So my final question then is if I have a script module with 100 functions at what point will the script module be compiled and brought into memory? The first time I call a function?

---

<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:** [September 9, 2020, 1:14pm UTC](https://forum.inductiveautomation.com/t/solved-how-do-global-variables-work-in-scripting/38484/7 "2020-09-09T13:14:46Z")

</div>

> [@bmeyers](#):
>
> The first time I call a function?

At least by then, but it could be earlier. You can put a logging statement in the top level of the script to see when it fires.
